Module: Torque::PostgreSQL::AutosaveAssociation

Defined in:
lib/torque/postgresql/autosave_association.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#save_belongs_to_many_association(reflection) ⇒ Object

Ensure the right way to execute save_collection_association and also keep it as a single change using build_changes



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/torque/postgresql/autosave_association.rb', line 26

def save_belongs_to_many_association(reflection)
  previously_new_record_before_save = (@new_record_before_save ||= false)
  @new_record_before_save = new_record?

  association = association_instance_get(reflection.name)
  association&.build_changes { save_collection_association(reflection) }
rescue ::ActiveRecord::RecordInvalid
  throw(:abort)
ensure
  @new_record_before_save = previously_new_record_before_save
end