我不明白为什么这不起作用?谁能解释一下?
scope :upcoming, where(:start_time > Time.zone.now)
和另一个:
scope :happening_now, where(Time.zone.now.between?(:start_time, :end_time))
我收到以下错误:
undefined method `<=>' for :end_time:Symbol (NoMethodError)
答案 0 :(得分:1)
因为您现在正在做的是比较符号和时间对象
试试这个:
scope :upcoming, where("start_time > ?", Time.zone.now)
如果要将哈希值传递给#where,则需要同时指定键和值。示例如下:
scope :example, where(:name => "chris")
答案 1 :(得分:1)
您应该使用Proc来执行这些条件。 否则您的查询将变为:
start_time > "time at which the server is started in production"
class User < ActiveRecord::Base
scope :upcoming, Proc.new { where(["start_time > ?", Time.zone.now])}
end