Tuesday, November 17, 2009

Use of Dirty Objects

Uses of Dirty Objects

Developer have some confusion that how they can get the previous value of the field after update.
So here the rails has a way called Dirty objects.

You can found Module Dirty in rails/activerecord/lib/active_record/dirty.rb
This is just for track unsaved attribute changes.
There are few methods in dirty module as below
* Changed
* Changed?
* Changes
* Included
And it has some constants

DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']

book = Book.find(1)

book.name = 'agiles'
book.changed? # => true
book.name_changed? # => true
book.name_was # => 'rails startup'
book.name_change # => ['rails startup', 'agiles']
book.name = 'agiles with rails'
book.name_change # => ['rails startup', 'agiles with rails']

You can found the details in http://api.rubyonrails.org/classes/ActiveRecord/Dirty.html

No comments:

Post a Comment

Contact Me for any help regarding rails/nodejs/php/mysql

Name

Email *

Message *