How to compare created_at reliably for Rails Activerecords -


basically, query saying record's created_at greater own created_at.

irb(main):025:0> u = user.first  user load (1.0ms)  select "users".* "users" limit 1 => #<user id: 1, email: "my@email.com", encrypted_password: "stuff...", created_at: "2012-02-01 18:56:45", updated_at: "2012-03-17 21:10:13">  irb(main):026:0> user.where("created_at > ?", u.created_at).include? u  user load (1.0ms)  select "users".* "users" (created_at > '2012-02- 01 18:56:45.740392') => true 

the query shown makes clear it's datetime formatting issue...when builds query it's rounding fractions of second. can modify query order created_at predictably/consistently?

i've poked around other questions discuss activerecord precision , suggest strftime, etc., can't work reliably. i'm using sqlite in dev (my console quote above) , postgres in production.

note: broader goal here add #next , #previous methods most/all of resources can iterate through them more in admin menus. if there's way achieve default order that's reliable i'm open it.

both methods work coded below when pass non-timestamp arguments :name, #next returns same object if pass no argument (defaulting :created_at)

def previous(column = :created_at)  self.class.first(:conditions => ["#{column} < ?", self.send(column)], :order => "#{column} desc") end  def next(column = :created_at)  self.class.first(:conditions => ["#{column} > ?", self.send(column)], :order => "#{column} asc") end 

for case should use id , don't worry date (it'll work more or less same anyway). it's naturally ordered, indexed, etc.

def previous(column = :id)   self.class.first(:conditions => ["#{column} < ?", self.send(column)], :order => "#{column} desc") end  def next(column = :id)   self.class.first(:conditions => ["#{column} > ?", self.send(column)], :order => "#{column} asc") end 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -