开发者

scoping in rails 3 with dates

开发者 https://www.devze.com 2023-03-23 15:31 出处:网络
I don\'t understand why this doesn\'t work? Can anyone explain? scope开发者_开发知识库 :upcoming, where(:start_time > Time.zone.now)

I don't understand why this doesn't work? Can anyone explain?

scope开发者_开发知识库 :upcoming, where(:start_time > Time.zone.now)

and another:

scope :happening_now, where(Time.zone.now.between?(:start_time, :end_time))

I receive the following error:

undefined method `<=>' for :end_time:Symbol (NoMethodError)


You should use Proc to perform these kind of conditions. Otherwise your query will become:

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


Because what you're doing right now is comparing a symbol and a Time object

Try this:

scope :upcoming, where("start_time > ?", Time.zone.now)

If you want to pass a hash into #where, you need to specify both key and value. Example below:

scope :example, where(:name => "chris")

0

精彩评论

暂无评论...
验证码 换一张
取 消