27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Attribute change tracking<br />

Of course this method will return true, because you changed your name attribute previously.<br />

In addition to this, you’re even able to see what the value was before the<br />

change using the *_was method:<br />

>> project.name_was<br />

=> nil<br />

This time, name was nil before you set it and so nil is returned. If it had a value<br />

before you created this object, it would return that instead. Finally, you can even get<br />

back an array containing the before and after values of the attribute by using the<br />

*_change method:<br />

>> project.name_change<br />

=> [nil, "Ticketee"]<br />

Now, what would these methods be used for? Well, any number of things, really. You<br />

could use it for only running a certain callback if a specific field had been changed, or<br />

you could use the *_change methods to log the changes to attributes in another table<br />

if you wish.<br />

549

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!