ruby-on-railsruby-on-rails-3ruby-on-rails-3.2mongoidmongoid3

check if object has been updated mongoid 3.x


1.9.3p448 :014 > l.reload
 => #<Lesson _id: 527246641d41c81d14000006, title: "ola"> 
1.9.3p448 :015 > l.changed?
 => false 
1.9.3p448 :016 > l.changes
 => {} 
1.9.3p448 :017 > l.previous_changes
 => {"title"=>["olaaaaa", "ola"]} 
1.9.3p448 :018 > l.changed?
 => false 
1.9.3p448 :019 > l.update_attributes(title: "olaaa")
 => true 
1.9.3p448 :020 > l.changes
 => {} 
1.9.3p448 :021 > l.changed?
 => false 

I have updated the attribute "title" but when I try l.changed? I get false.

I know the new_record? method, to know if a object is a new object but I need to know if a object is updated.

I would like to know, how can I know if a object has been updated with mongoid 3.x?


Solution

  • When you reload, save, update, the changes are moved to previous_changes. You also have access to the changes on callbacks, otherwise you will have to use previous_changes . Thats by design, to be consistent with ActiveRecord.

    previous_changes is documented here along all the other attribute change observation stuff.

    there are also methods for manipulating & removing changes