Perhaps I’m missing something here, but here’s a method that I often add into my Rails models
def self.find_or_create opts first(:conditions => opts) || create(opts) end
It’s use is simple:
Membership.find_or_create :channel_id, :user_id
Now there is a dynamic finder self.find_or_create_by_XXX, but it isn’t useful in the same way.
Is there a reason why something like that method isn’t in AR core ?
Advertisement

December 3, 2009 at 7:29 pm
Ninja you saved me badly!
My use:
Collection.find_or_create_by_item_id_and_user_id(params[:collection])
Thanks so much!