views:

46

answers:

2

I'm creating a Rails application which uses MySQL. I have a table in my DB like this:

  create_table "pastes", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.string   "syntax"
    t.boolean  "private"
    t.datetime "expire"
    t.string   "password"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

I want to show only the non-expired pastes to people, so I do this:

@pastes = Paste.find(:all, :conditions => "expire < '#{Time.now.to_s(:db)}'")

However, even that returns ALL pastes. Not just those that are not expired yet. Can anyone help me? Thanks

Oh, changing < to > returns no pastes, not even the non-expired ones :(

A: 

I solved it already. It had to do with timezones. This works:

@pastes = Paste.find(:all, :conditions => "expire > '#{Time.now.utc.to_s(:db)}'")
#                                                               ^^^ this does it
Time Machine
If you rewrite your condition to :conditions => ['expire > ?', Time.now] you don't need to convert to utc as ActiveRecord will handle that for you.
Corey
+1  A: 
John Topley
This only causes confusion, since the expired pastes are only stored for spam-fighting. I'll never display them.
Time Machine
Who said anything about displaying them? Scopes are just a way of obtaining a subset of records using a convenient syntax.
John Topley