Two Interesting Things About ActiveRecord Validations
Posted by mhagedorn on October 4th, 2007 filed in RailsYou know how people always tell you “once you know how this works, you can break the rules when you need”. I had to apply this maxim recently. I had a situation where I needed to create new records on a form (so they would display as lineitems), but needed to defer validating these items until later. (In my case it was time sheet entries, but it doesnt really matter.) I struggled with how to do this until I stumbled on these two gems of knowledge. (which I probably should have remembered anyway)
ActiveRecord#save(false) you can force active record to save and ignore validation errors
ActiveRecord#update_attribute This does the same thing as above, but on a per attribute basis
Very handy. And yes.. I know that this is “breaking the rules” because I am skipping validation. But in this particular case.. it makes sense
One Response to “Two Interesting Things About ActiveRecord Validations”
Leave a Comment
You must be logged in to post a comment.
October 5th, 2007 at 4:30 pm
Wow. Not sure why I hadn’t bumped into these before but boy are they useful. Especially for testing.