そんなに使う局面は無いだろうが、自分用にメモ。
Railscasts - Tracking Attribute Changes
.changed?
modelに変更があったか
例)true/false
_changed?
attributeに変更があったか
例)true/false
._was
変更前のattributeの値を表示
例)"Blue Scissors"
._change
変更前後のattributeの値を表示
例)["Blue Scissors", "Scissors"]
.changed
変更されたattribute名を表示
例)["name"]
.changes
変更されたattribute名・値を表示
例){"name" => ["Blue Scissors", "Scissors"]}
._will_change!
変更するattributeを明示。
例)
_will_change
使いどころがわからなかったので
script/consoleで確かめてみた
$ script/console --sandbox Loading development environment in sandbox (Rails 2.3.5) Any modifications you make will be rolled back on exit >> a = Article.first Article Load (29.2ms) SELECT * FROM "articles" LIMIT 1 => #<Article id: 1, title: "Title", content: "Contents", created_at: "2010-06-13 17:19:56", updated_at: "2010-06-15 17:05:57"> >> a.title.upcase! => "TITLE" >> a.changed? => false >> a.changes => {}
_will_changeを使用しないと変更と認識されない。
なるほど。代入以外での変更時に使うのか。