Module: ActiveRecordUpsert::ActiveRecord::PersistenceExtensions
- Defined in:
- lib/active_record_upsert/active_record/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #_upsert_record(upsert_attribute_names = changed, arel_condition = nil) ⇒ Object
- #upsert(*args) ⇒ Object
- #upsert!(attributes: nil, arel_condition: nil, validate: true) ⇒ Object
Instance Method Details
#_upsert_record(upsert_attribute_names = changed, arel_condition = nil) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 35 def _upsert_record(upsert_attribute_names = changed, arel_condition = nil) existing_attributes = arel_attributes_with_values_for_create(self.attributes.keys) values = self.class.unscoped.upsert(existing_attributes, upsert_attribute_names, [arel_condition].compact) @new_record = false values end |
#upsert(*args) ⇒ Object
28 29 30 31 32 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 28 def upsert(*args) upsert!(*args) rescue ::ActiveRecord::RecordInvalid false end |
#upsert!(attributes: nil, arel_condition: nil, validate: true) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_record_upsert/active_record/persistence.rb', line 5 def upsert!(attributes: nil, arel_condition: nil, validate: true) raise ::ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly? raise ::ActiveRecord::RecordSavedError, "Can't upsert a record that has already been saved" if persisted? validate == false || perform_validations || raise_validation_error values = run_callbacks(:save) { run_callbacks(:create) { attributes ||= changed attributes = attributes + + _upsert_record(attributes.map(&:to_s).uniq, arel_condition) } } # When a migration adds a column to a table, the upsert will start # returning the new attribute, and assign_attributes will fail, # because Rails doesn't know about it yet (until the app is restarted). # # This checks that only known attributes are being assigned. assign_attributes(values.first.to_h.slice(*self.attributes.keys)) self end |