ruby on rails - Querying changed model attributes in rake task -


i'm performing periodic rake task update attributes in user model. i'd execute mailer if particular attribute has changed, can't call attribute_changed? module doesn't know instance query - you'd in model after_save callback.

so have this:

task :my_task => :environment       user.all.each |user|       @howmanythings = user.things.size      user.update_attribute(:total_things, @howmanythings)       user.save       //this bit not work:       if user.total_things_changed? == true        send mailer      end    end     end 

at moment, user.total_things_changed? returning false, regardless of whether rake task has updated attribute. in short, how execute after_save callback (or equivalent) within task? many in advance.

activerecord::dirty tracks unsaved attribute changes. moment save model, foo_changed? methods start returning false.

note update_attribute saves model.

you like

@howmanythings = user.things.size user.total_things = user.things.size  if user.total_things_changed? == true   # send mailer end  user.save 

to make work expect.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -