ruby on rails - ActiveRecord on create, update of any model -


without sti (single table inheritance), there way check if model has been created or updated , determine model , attributes changed/updated on model?

i.e. output of running rails server shows http traffic , queries being run on db. cache invalidation purposes, i'm trying write code requires me know this.

i'm looking after_create , after_update, rather on 1 model, need have universal after create , after update , have ability determine model created or updated.

can done in activerecord? if so, how?

if you're not changing logic of models, isn't universal hook, wouldn't want in activerecord::base. duck typing bad.

it sounds have common behavior , way handle module (or activesupport::concern).

example modified here (assuming you're running rails 3+)

module maintainaninvariant   # common logic goes here   extend activesupport::concern    included     after_save :maintain_invariant_i_care_about   end    def maintain_invariant_i_care_about     do_stuff_pending_various_logic   end end 

now each class shares logic explicitly include it, adding semantic value

class oneofthemodelswiththislogic < activerecord::base   include maintainaninvariant end  class anothermodelwithcommonlogic < activerecord::base   include maintainaninvariant end 

as rest of answer, how know what's changed, you're looking activemodel::dirty methods. these allow check changed in models:

person.name = 'bill' person.name_changed? # => false person.name_change   # => nil person.name = 'bob' person.changed # => ["name"] person.changes # => {"name" => ["bill", "bob"]} 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -