Well, I couldn't rest until I figured out an implementation of
create_or_update (see version 1 above) using the oh-so-nifty
ActiveRecord::Relation framework.

Here it is:

class ActiveRecord::Base

  def self.create_or_update(relation, attrs_to_update)
    if (incumbent = relation.first).nil?
      relation.create!(attrs_to_update)
    else
      incumbent.update_attributes(attrs_to_update)
      incumbent
    end
  end

end

What's nice about this is that the relation can be a more expressive 
selector than a simple hash

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to