sjsc
sjsc

Reputation: 4652

Ruby on Rails: setting future "publishing" dates for blog postings

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?

Upvotes: 2

Views: 1072

Answers (2)

Luke Francl
Luke Francl

Reputation: 31474

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.

Upvotes: 3

Ron Gejman
Ron Gejman

Reputation: 6215

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

Upvotes: 0

Related Questions