Class: RailsCoreExtensions::TransferRecords
- Inherits:
-
Object
- Object
- RailsCoreExtensions::TransferRecords
- Defined in:
- lib/rails_core_extensions/transfer_records.rb
Instance Method Summary collapse
-
#initialize(parent, klass, options = {}) ⇒ TransferRecords
constructor
A new instance of TransferRecords.
- #transfer_from(objects) ⇒ Object
Constructor Details
#initialize(parent, klass, options = {}) ⇒ TransferRecords
Returns a new instance of TransferRecords.
3 4 5 6 7 |
# File 'lib/rails_core_extensions/transfer_records.rb', line 3 def initialize(parent, klass, = {}) @parent = parent @klass = klass @options = end |
Instance Method Details
#transfer_from(objects) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rails_core_extensions/transfer_records.rb', line 9 def transfer_from(objects) record_ids = objects.flat_map { |o| o.send(@klass.name.underscore + '_ids') } unless record_ids.empty? @options[:foreign_key] ||= @parent.class.name.underscore + '_id' = @options.except(:foreign_key) [@options[:foreign_key]] = @parent.id @klass.where(id: record_ids).update_all() end end |