13
Jul
2006
Some Interesting Dynamic Finders
find_or_create_by_X: As obvious by name this dynamic finder method allows you to find or create a new record on the basis of attributes passed. A very good example by DHH himself goes like this…
class Account < ActiveRecord::Base
has_many :people do
def find_or_create_by_name(name)
first_name, *last_name = name.split
last_name = last_name.join " "
find_or_create_by_first_name_and_last_name(first_name, last_name)
end
end
end
person = Account.find(:first).people.find_or_create_by_name("David Heinemeier Hansson")
person.first_name # => "David"
person.last_name # => "Heinemeier Hansson"
find_or_initialize_by_X : This new method in available in edge rails now. Its almost similar to the find_or_create_by_X finder. The basic difference is that this method unlike find_or_create_by_X only initialize the object and do not saves it.


Working with Vinsol has been one of the most rewarding and productive collaborations I've ever had in the technology industry. Manik is an intelligent and honest engineer with a great faculty for open and clear communication, and his team include some of the swiftest and keenest developers I've ever had the pleasure to work with. They're great value for money, excellent people who are a joy to know and interact with, and above all they really know their stuff