Class: ActiveRecord::Base
- Inherits:
-
Object
- Object
- ActiveRecord::Base
- Defined in:
- lib/create_or_update.rb
Class Method Summary collapse
-
.create_or_update(options = {}) ⇒ Object
given a hash of attributes including the ID, look up the record by ID.
Class Method Details
.create_or_update(options = {}) ⇒ Object
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 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.
Returns the record.
10 11 12 13 14 15 16 17 18 |
# File 'lib/create_or_update.rb', line 10 def self.create_or_update( = {}) id = .delete(primary_key.to_sym) record = send("find_by_#{primary_key}", id) || new record.id = id record.attributes = record.save! record end |