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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -