Find or Create

Posted by Matt Williams

I found myself recently typing code like:
1
2

foo = Bar.find(:first, :conditions => {:name => "Fred"}) || Bar.new(:name => "Fred")
As it turns out, there's a rails method for this: Model.find_or_create_by_COLUMN, where COLUMN is the name of the column.
Comments

Leave a response