Skip to content

Instantly share code, notes, and snippets.

@jamiew
Created August 23, 2008 19:09
Show Gist options
  • Save jamiew/6935 to your computer and use it in GitHub Desktop.
Save jamiew/6935 to your computer and use it in GitHub Desktop.
# wise's mod, from: http://blogwi.se/post/47083009/slightly-pimped-activerecord-create-or-update
# original from Rails Spikes: http://railspikes.com/2008/2/1/loading-seed-data
def self.create_or_update(options = {})
id = options.delete(:id)
conditions = options.delete(:conditions)
record = id ? find_by_id(id) : find(:first, :conditions => conditions) || new
options.each_pair { |key, value| record[key] = value }
record.save!
record
end
# then do
Venue.create_or_update(:id => 1, :name => "Coffman Union")
Venue.create_or_update(:id => 2, :name => "Alumni Center")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment