marcus
marcus

Reputation: 1

config.cache_classes messing with DateTime in model

Hey guys, im having some problem here due to rails class caching. I have this named_scope

named_scope :current, :conditions => "starts_at <= '#{Time.now.utc.to_formatted_s(:db)}' and finishes_at >= '#{Time.now.utc.to_formatted_s(:db)}'"

The condition Time is not refreshing, all requests are done with the same Time, probably the first used.

Is there a way to work around it?

Upvotes: 0

Views: 106

Answers (1)

jwarchol
jwarchol

Reputation: 1916

In order to do that you need to put the scope in a lambda

named_scope :current, lambda { :conditions => ["starts_at <= ? AND finishes_at >= ?", Time.now.utc, Time.now.utc] }

By having it in a lambda the code is executed each time the scope is called, so you always get the current time.

Upvotes: 1

Related Questions