ruby - using the _was method in rails -
so trying use _was method on attribute. lets have object person
:
person.name = "new name" person.name_was = "old name"
if call person.changed?
return ["name"]
.
so question is: can somehow use values person.changed
this:
person.changed.each |x| person.x => returns undefined value x person['x'] => returns new name person["#{'x'}_was"] => errors end
is there way can use _was
method string variable x
?
i'm not quite sure example code trying achieve, think looking #changes method;
person = person.create(first_name: 'john', last_name: 'doe', sex: 'male') person.first_name = 'jane' person.sex = 'female' person.changes.each |attribute, change| puts attribute puts change.first puts change.last end # => 'first_name' # => 'john' # => 'jane' # => 'sex' # => 'male' # => 'female'
person.changes returns hash changes where; { attribute_name => [before, after] }
Comments
Post a Comment