module Torque::PostgreSQL::AutosaveAssociation

Public Instance Methods

save_belongs_to_many_association(reflection) click to toggle source

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

# 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