Dynamic conditions for associations in Rails 3

If you are using dynamic conditions in Rails 3, like Time.now or 1.week.ago you should use a Proc for that condition, like this:

has_many :rented_cars ...,
         :conditions => proc { "'#{Date.today.to_s(:db)}' <= end_rent_date" }

This way, Date.today will be called every time you perform a query.

If you would have done it like in the following example, there will be cases when the results you’ll get are not expected since the condition will have the day you started the Rails application and Date.today won’t be called again.

has_many :rented_cars ...,
         :conditions => ["'#{Date.today.to_s(:db)}' <= end_rent_date"]

I hope this helps.