Reputation: 973
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 @user.foo_changed?
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 ?
Upvotes: 1
Views: 4077
Reputation: 10251
try :
if @user.foo.age == params[:user][:foo][:age]
# not changed
else
# changed
end
OR
@user.foo.age_changed? # this will work in case foo is associated(nested) model with user and foo table has "age" field
Upvotes: 1
Reputation: 4615
Ruby on Rails provide something methods to do this:
object.attribute_changed?
for example:
User.first.name_changed?
Did you tried this?
@user.age_changed?
You can find more here
Upvotes: 0