Hi,
I am developing a rails 3.0.3 application and
accepts_nested_attributes_for method is giving me pains.
To simplify the issue, I created a new app and generated 2 models.
users
Hi,
I am developing a rails 3.0.3 application and
accepts_nested_attributes_for method is giving me pains.
To simplify the issue, I created a new app and generated 2 models.
users
The docs seem to contradict each other. First...
# You may also set a :reject_if proc to silently ignore any new record
# hashes if they fail to pass your criteria. For example, the previous
# example could be rewritten as:
But then....
# Allows you to specify a Proc or a Symbol pointing to a method
# that checks whether a record should be built for a certain attribute
# hash. The hash is passed to the supplied Proc or the method
# and it should return either +true+ or +false+. When no :reject_if
# is specified, a record will be built for all attribute hashes that
# do not have a <tt>_destroy</tt> value that evaluates to true.
# Passing <tt>:all_blank</tt> instead of a Proc will create a proc
# that will reject a record where all the attributes are blank.
The code says the second is true... from active_record/nested_attributes.rb around line 376...
It loops through all the nested attributes...
- if the 'id' is blank and we don't reject the record, then build it.
- else we have an 'id' so find the record and if we don't reject it, add it to the target and *then* mark it for destruction.
Unless I'm reading it wrong
attributes_collection.each do |attributes|
attributes = attributes.with_indifferent_access
if attributes['id'].blank?
unless reject_new_record?(association_name, attributes)
association.build(attributes.except(*UNASSIGNABLE_KEYS))
end
elsif existing_record = existing_records.detect { |record| record.id.to_s == attributes['id'].to_s }
association.send(:add_record_to_target_with_callbacks, existing_record) if !association.loaded? && !call_reject_if(association_name, attributes)
assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
else
raise_nested_attributes_record_not_found(association_name, attributes['id'])
end
end