Module: Worldwise::ActiveRecord::ClassMethods
- Defined in:
- lib/worldwise/active_record/class_methods.rb
Instance Method Summary collapse
-
#create_or_update(options = {}) ⇒ Object
Given a hash of attributes including the ID, look up the record by ID.
Instance Method Details
#create_or_update(options = {}) ⇒ Object
Given a hash of attributes including the ID, look up the record by ID. If it exists, it is updated with the given options. If it does not exist, it is created with the rest of the options.
Raises an exception if the record is invalid to ensure seed data is loaded correctly.
Returns the record.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/worldwise/active_record/class_methods.rb', line 13 def create_or_update( = {}) id = .delete('id') translations_attributes = .delete('translations_attributes') || {} record = find_by_id(id) || new record.id = id record.attributes = translations_attributes.each do |ta| ta.delete("#{record.class.name.underscore}_id") rta = record.translations.find_by_locale(ta['locale']) || record.translations.build rta.send("#{record.class.name.underscore}_id=", record.id) rta.attributes = ta rta.save! unless record.new_record? end record.save! record end |