Nate Bird
Nate Bird

Reputation: 5335

scoping in rails 3 with dates

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)

Upvotes: 0

Views: 187

Answers (2)

Arun Kumar Arjunan
Arun Kumar Arjunan

Reputation: 6857

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

Upvotes: 1

Chris Ledet
Chris Ledet

Reputation: 11628

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")

Upvotes: 1

Related Questions