【问题标题】:<ActiveRecord::QueryMethods::WhereChain error on a scope that was working<ActiveRecord::QueryMethods::WhereChain 在工作范围内出错
【发布时间】:2019-01-09 19:26:21
【问题描述】:

我已经成功运行了一段代码,但它刚刚开始抛出错误 - 我们已经改变了数据进入数据库的方式,但我认为这更有可能是编程问题,或者我已经错误地更改了它,但看不到在哪里。

问题似乎是由范围引起的,我们正在运行 Rails 4.2.2

完整的错误是

undefined method `call' for 
 ActiveRecord::QueryMethods::WhereChain:0x007feb8f5c49f0> Did you mean? caller

app/controllers/orders_controller.rb:158:in `weekly_sales_report'

在我的 orders_controller 第 158 行

@sales_this_week_bysales = Order.select("COUNT(order_id) AS nosales, AVG(net_amount) AS avgsale,SUM(sale_cost) AS sale_cost, SUM(net_amount) AS weeklysalestotal, owner_id AS salesman").sales.where.(date_of_sale: (@startdate .. @enddate)).with_salesman.group(:owner_id)

在我的 orders.rb 模型中,我使用了以下范围

scope :with_salesman, -> { joins(:pipe_records).where(pipe_records: {pipe_part_id: 1}).where.not(pipe_records: {owner_id: nil}) }

scope :sales, -> {where ("orders.order_ref <>'' and date_of_sale IS NOT NULL ")}

我将范围重写到下面,但仍然出现相同的错误

scope :with_salesman, -> { joins(" INNER JOIN pipe_records ON pipe_records.order_id = orders.id WHERE pipe_records.pipe_part_id =1 AND pipe_records.owner_id <>'' ") }

我还从 sales.where on 158 中删除了 WHERE startdate 条件,它返回了一个不同的错误,但似乎范围不再正确传递或由于记录错误而返回错误??

我现在不确定发生了什么,我回到我的遥控器并复制了几天前的代码副本,这也引发了同样的错误,但它工作正常。最近,我们通过一种新形式添加了许多新记录,该形式绕过并更改了可能是罪魁祸首创建的日期。我将不胜感激任何建议或新鲜的眼睛。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    只需删除where 和括号之间的句点。

    where.(date_of_sale: (@startdate..@enddate))
    

    ruby

    receiver.()
    

    receiver.call() 的简写,因此您收到的错误

    【讨论】:

    • 世界上最大的掌心....看了一百遍都错过了,谢谢
    【解决方案2】:

    你可能想删除where之后的那个句号:

    where.(date_of_sale: (@startdate .. @enddate))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多