【问题标题】:Rails 3 - undefined method `where' for #<User:0x00000104c69b98>Rails 3 - #<User:0x00000104c69b98> 的未定义方法“where”
【发布时间】:2012-01-19 10:08:42
【问题描述】:

我有以下辅助方法:

  def orders_chart_series(orders, start_time)
    orders_by_day = orders.where(:created_at => start_time.to_date..Date.today).
                    group("date(created_at)").
                    select("created_at, count(id) as total_price")
    (start_time.to_date..Date.today).map do |date|
      order = orders_by_day.detect { |order| order.created_at.to_date == date }
      order && order.total_price.to_f || 0
    end.inspect
  end

我仍然收到此线程名称中的错误。我尝试打印 orders 参数的内容,得到了这个:

#<User:0x00000104c69b98>

这个问题是什么意思?我试图寻找这个问题的解决方案,但我仍然知道如何解决它。 我也尝试将命令 where 替换为 conditions,但还是一样。

对于这个问题的每一个帮助,我都会非常感激,谢谢。

【问题讨论】:

    标签: ruby ruby-on-rails-3 helper where conditional-statements


    【解决方案1】:

    您尝试在 User-instance 上调用 where,而不是在 User-class 上(where 是一个类方法)。您可以将orders.where 更改为orders.class.where

    (在一个名为 orders 的变量中拥有一个 User 实例也有点令人困惑,但这可能只是我。)

    【讨论】:

    • 谢谢,它对我有用。这部分代码来自 Ryan Bates 的教程,关于奇怪的名字 - 我刚试过,这个例子是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多