views:

180

answers:

2

Hello,

I'm trying to set blog postings to publish at certain dates in the future. I have in my Posting model:

 named_scope :published, :conditions => ["publish_at <= ?", Time.now]

I'm using this in my controller to call the published postings:

  @postings = Posting.published

The development server works fine, but I believe the production server needs me to refresh the cache (using "pkill -9 dispatch.fcgi") or I won't see the new postings when it's supposed to publish.

Is there any way to set future times for the postings' publishing dates correctly on the production server? Do I have to refresh the cache every time?

A: 

Check to see if you have any of the following statements in your production environment:

 ActionController::Base.cache_store = :memory_store

OR

 ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"

OR

 ActionController::Base.cache_store = :mem_cache_store

OR any other setting for ActionController::Base.cache_store

Ron Gejman
Hi Ron. I don't have any of those. I only these:config.cache_classes = trueconfig.action_controller.consider_all_requests_local = falseconfig.action_controller.perform_caching = true
sjsc
Hmm, I'm not sure why you're seeing this effect.
Ron Gejman
It seems like it doesn't publish instantly (I did see a post whose publishing date was a few hours prior publish okay). I'm thinking that maybe it just takes a few hours or so before Rails resets or something.
sjsc
My first thought was that it was taking a while to refresh SQL cache, but those are supposed to be expunged at the end of each request. Are you sure you don't have any caching set up? No memcached or similar?
Ron Gejman
By "taking a while" I mean "until the process died" since you mentioned that killing the process was effective.
Ron Gejman
I don't believe I have any cache set up (just those 3 lines above). It's probably my code though so I'll try to pinpoint the issue somehow. Thanks a bunch for the great help Ron.
sjsc
+3  A: 

You are correct, because the named scope is evaluated when the class loads.

You should re-write it to be dynamic or (maybe better) use the database's now() function.

Either of these should work:

named_scope :published, lambda { {:conditions => ["publish_at <= ?", Time.now]} }

Note how this uses a lambda to always return the current time in the conditions hash.

named_scope :published, :conditions => "publish_at <= now()"

This is database dependent (the above should work for MySQL) but probably a tiny bit faster.

Luke Francl
Interesting. Doesn't seem to be described that way here: http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html
Ron Gejman
Thanks so much Luke! I'll test that and see if it works. Thank you so much.
sjsc
Thanks Luke! You're lifesaver! It works perfectly =) Thank you!
sjsc
No problem. I ran into a very similar issue a few months ago so I'd already been through it.
Luke Francl
I just tried this, and I needed:named_scope :published, lambda { { :conditions => ["publish_at <= ?", Time.now] } }2 sets of brackets. Seems to work for me ....
James
Oops, you're right, I've run into that problem before. I'll edit the answer.
Luke Francl