published_at on Heroku/PostgreSQL

14 views
Skip to first unread message

zver

unread,
Sep 26, 2011, 10:18:31 AM9/26/11
to Ruby on Rails: Talk
Hi all!

I have one really annoying problem - I hope you can give me some
advice...

1) I have a model "Article". Article has among other fields
created_at, published_at and publish_date field.
I want the list of "published" articles if the publish_date is <= then
current time/date.

2) In my Article model I have following scope:
https://github.com/otobrglez/sveze-jabolko/blob/master/app/models/article.rb#L26

3) Settings for my timezone in rails:
https://github.com/otobrglez/sveze-jabolko/blob/master/config/application.rb#L30

4) I also set TZ for my Heroku instance like so:
heroku config:add TZ="Europe/Ljubljana"

5) This is my last migration:
https://github.com/otobrglez/sveze-jabolko/blob/master/db/migrate/20110922105754_change_publish_date.rb

I use Heroku and PostgreSQL. But the darn thing just does not work as
it should :| What should I do that this thing will work as expected?

I want article to be visible after the "publish_date".

Thanks for any advice and have a nice day!

- Oto

Peter Vandenabeele

unread,
Sep 27, 2011, 4:24:43 AM9/27/11
to rubyonra...@googlegroups.com
On Mon, Sep 26, 2011 at 4:18 PM, zver <otob...@gmail.com> wrote:
Hi all!

I have one really annoying problem - I hope you can give me some
advice...

1) I have a model "Article". Article has among other fields
created_at, published_at and publish_date field.
I want the list of "published" articles if the publish_date is <= then
current time/date.

2) In my Article model I have following scope:
https://github.com/otobrglez/sveze-jabolko/blob/master/app/models/article.rb#L26

Maybe you forgot to use a block in your scope (in that case the Time.now is
evaluated only 1 time in production, at the moment the class is loaded, not at
the time the scope is called). ?

From


<quote>
Note that scopes defined with scope will be evaluated when they are defined, rather than when they are used. For example, the following would be incorrect:

class Post < ActiveRecord::Base
  scope :recent, where('published_at >= ?', Time.now - 1.week)
end
The example above would be ‘frozen’ to the Time.now value when the Post class was defined, and so the resultant SQL query would always be the same. The correct way to do this would be via a lambda, which will re-evaluate the scope each time it is called:

class Post < ActiveRecord::Base
  scope :recent, lambda { where('published_at >= ?', Time.now - 1.week) }
end 
</quote>

HTH,

Peter
Reply all
Reply to author
Forward
0 new messages