Get the current Rails variable in before_save

Is there a way to get the current variable? let's say that I have the "title" attribute in the method and I want to register with the old value of the variable?

How can i do this?

+3
source share
2 answers

ActiveRecord provides a whole set of methods that work with Dirty attributes .

In your case, you can call title_wasto get the most recent saved version of this variable

+10
source

, changed_attributes, , ( , - ).

, old_name:

a = YourModel.where(:title => "old_name")
a.title = "new_title"
a.changed_attributes

: { "" = > "old_name" }

:

a.save
a.changed_attributes

: {}

0

Source: https://habr.com/ru/post/1764382/


All Articles