user4965201 user4965201 - 1 year ago 72
Ruby Question

Ruby on Rails detect changes in attributes in update

Hi I have a Rails app in which i wanted to know if an attribute is changed which occurs within the hash
let me explain i have params for @user

"foo"=> {"name"=> "name1", "age"=> "15", "height"=> "120"}

now when i change only attribute age i want to know if the age is changed

what i tried is
but it will give true whenever anything changes in the foo hash but i want it to be true only when the age is changed . How can i achieve this ?

Answer Source

try :

if == params[:user][:foo][:age]
     # not changed
    # changed

OR  # this will work in case foo is associated(nested) model with user and foo table has "age" field
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download