Rails - ActiveRecord Dirty - Getting associated objects from the changes hash -


i'm working on audit trail of sorts app user can see being changed throughout system.

i have hash of changes activerecord dirty, follows:

{"ingredient_type_id"=>[nil, 199575006], "name"=>[nil, "asdfg"], "amount"=>[nil, 3.0], "unit"=>[nil, "x"], "notes"=>[nil, "asdf"]} 

this works great , can parse need output , create database records info.

i have 1 question - how can associated objects this? in case, ingredient_type? want output like:

"ingredient type changed #{ingredienttype.find(199575006).name}."

but i'm not sure how parse hash on dynamic basis that.

pretty way you've suggested i'd have thought, don't need parse hash changes, dirty gives more that

if ingredient_type_id_changed?   unless ingredient_type_id.blank?     ingredient_name = ingredienttype.find(ingredient_type_id).name           else     ingredient_name = 'blank'           end end 

you might able ingredient_type.name, not sure @ point if active record dirty let go through association. if test (or if else knows) let me know


Comments

Popular posts from this blog

css - I want to align grid in center -

Contact Form PHP Email Script -

python - SWIG function not printing output -