db_populate/lib/create_or_update.rb

27 lines
918 B
Ruby
Raw Normal View History

2008-09-16 01:08:50 +00:00
class ActiveRecord::Base
# given a hash of attributes including the ID, look up the record by ID.
# uses whatever the PK of the model is to do the lookup
2008-09-16 01:08:50 +00:00
# If it does not exist, it is created with the rest of the options.
# If it exists, it is updated with the given options.
#
# Raises an exception if the record is invalid to ensure seed data is loaded correctly.
# Pass :perform_validations => false to skip validations in the model.
#
2008-09-16 01:08:50 +00:00
# Returns the record.
def self.create_or_update(options = {})
id = options.delete(primary_key.to_sym)
validate = options.delete(:perform_validations){|k| true}
record = send("find_by_#{primary_key}", id) || new
2008-09-16 01:08:50 +00:00
record.id = id
if validate
2009-11-26 17:45:28 +00:00
record.attributes = options
record.save!
else
2009-11-26 17:45:28 +00:00
options.each do |key, value|
record.send("#{key}=", value)
end
record.save(false)
end
2008-09-16 01:08:50 +00:00
record
end
end