views:

654

answers:

2

I want to use a dynamic scope in Rails where I filter by date. I want to do something like this:

Foo.scoped_by_created_on(>the_future).scoped_by_created_on(<the_past).find(:all)

Is this possible or am I stuck writing a :conditions hash?

+1  A: 

You can't do this with scoped_by however you can make your own scopes for this.

named_scope :created_on_before, lambda { |time| { :conditions => ["created_on < ?", time] } }
named_scope :created_on_after, lambda { |time| { :conditions => ["created_on > ?", time] } }

Alternatively check out Searchlogic which offers named scopes for this.

Foo.created_on_greater_than(the_future).created_on_less_than(the_past)
ryanb
thank! very helpful
*thanks!very rarely are the code snippets good enough to be copied and pasted verbatim. Awesome.
A: 

I have a plugin called by_star that offers you both a past and future method (amongst others) so you can do:

@foos = Foo.past(some_time) do
  Foo.future(some_other_time)
end
Ryan Bigg
thanks. ill take a look a that.