Dirty field checking

September 19th, 2008

Now that we’ve upgraded to rails 2.1, I have a handy new activerecord tool called Dirty that tracks unsaved attribute changes.

Rather than the code below:

     newscoretype = 1
     oldscoretype = score.scoretype_id
     if oldscoretype == newscoretype...

 

I can instead write:

     if score.scoretype_id_changed?...

 

I still have easy access to the old value using _was

     oldscoretype = score.scoretype_id_was

 

There are also methods that query the dirty state of the whole model, e.g. score.changed?

Love it.

Entry Filed under: Ruby on Rails

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


RSS Berkman Gender & Tech

RSS Tweets

Tags

Meta