2008-09-16 01:08:50 +00:00
|
|
|
class ActiveRecord::Base
|
2009-03-21 11:28:27 +00:00
|
|
|
# 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.
|
2009-11-26 16:54:30 +00:00
|
|
|
# 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 = {})
|
2009-03-21 11:28:27 +00:00
|
|
|
id = options.delete(primary_key.to_sym)
|
2009-11-26 17:09:31 +00:00
|
|
|
validate = options.delete(:perform_validations){|k| true}
|
2009-03-21 11:28:27 +00:00
|
|
|
record = send("find_by_#{primary_key}", id) || new
|
2008-09-16 01:08:50 +00:00
|
|
|
record.id = id
|
2009-11-26 16:54:30 +00:00
|
|
|
if validate
|
2009-11-26 17:45:28 +00:00
|
|
|
record.attributes = options
|
2009-11-26 16:54:30 +00:00
|
|
|
record.save!
|
|
|
|
else
|
2009-11-26 17:45:28 +00:00
|
|
|
options.each do |key, value|
|
|
|
|
record.send("#{key}=", value)
|
|
|
|
end
|
2009-11-26 17:09:31 +00:00
|
|
|
record.save(false)
|
2009-11-26 16:54:30 +00:00
|
|
|
end
|
2008-09-16 01:08:50 +00:00
|
|
|
record
|
|
|
|
end
|
2011-08-25 05:00:30 +00:00
|
|
|
|
|
|
|
# for PostgreSQL (and others?) we must reset the id sequential to the next
|
|
|
|
# available highest number. This happens because the create_or_update
|
|
|
|
# creates records with an id already set, bypassing PostgreSQL's automatic
|
|
|
|
# incrementing sequential id counter
|
|
|
|
# Otherwise, we get the dreaded "duplicate key value violates unique constraint" error
|
|
|
|
def self.postgresql_reset_id_seq
|
|
|
|
connection.execute("SELECT setval('#{self.table_name}_id_seq'::regclass, MAX(id)) FROM #{self.table_name}")
|
|
|
|
end
|
2008-09-16 01:08:50 +00:00
|
|
|
end
|