diff --git a/README.md b/README.md index 9027b9c..9bc058e 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,10 @@ I shamelessly stole this idea from - [Staging Changes Within Vim](git/staging-changes-within-vim.md) - [Verbose Commit Message](git/verbose-commit-message.md) +## rails + +- [Attribute Was](rails/attribute-was.md) + ## ruby - [Limit Split](ruby/limit-split.md) diff --git a/rails/attribute-was.md b/rails/attribute-was.md new file mode 100644 index 0000000..1bac502 --- /dev/null +++ b/rails/attribute-was.md @@ -0,0 +1,26 @@ +# Attribute Was + +When modifying the attributes of an `ActiveRecord` object, you may want to +know what values the modified attributes used to have. `ActiveRecord` gets +some handy methods from the `ActiveModel::Dirty` module that allow you to +check these values out even if the object's attributes were changed before +you received it (though you are out of luck once it has been saved). +Just add `_was` onto the end of the attribute in question. + +```ruby +>> pokemon.name +=> "Charizard" +>> pokemon.name = "Squirtle" +=> "Squirtle" +>> pokemon.name +=> "Squirtle" +>> pokemon.name_was +=> "Charizard" +>> pokemon.save + ... +=> true +>> pokemon.name_was == pokemon.name +=> true +``` + +[source](http://api.rubyonrails.org/classes/ActiveModel/Dirty.html)