Module: Para::Ext::ActiveRecord::NestedAttributes

Defined in:
lib/para/ext/active_record_nested_attributes.rb

Instance Method Summary collapse

Instance Method Details

#call_reject_if(association_name, attributes) ⇒ Object

Handle rejected nested records that have a fake id, which means that the resource was nevertheless created by Para during input params parsing. Therefore, we clean those records when they’re rejected.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/para/ext/active_record_nested_attributes.rb', line 16

def call_reject_if(association_name, attributes)
  super.tap do |rejected|
    if rejected && attributes['id'].to_s.match(/\A__/)
      records = association(association_name).target

      if ::ActiveRecord::Base === records
        records.destroy
      elsif (record = records.find { |res| res.id == attributes['id'] })
        records.delete(record)
      end
    end
  end
end