Is this a bug of accepts_nested_attributes_for?

Hi,

I’ve been digging a bug for 3 days.
I pin-pointed the problem line of codes.
It’s not my code but the activerecord code.

The problem is with the
activerecord-3.0.3/lib/active_record/nested_attributes.rb

line# 376: association.send(:add_record_to_target_with_callbacks,
existing_record) if !association.loaded? &&
!call_reject_if(association_name, attributes)

I realized that the line was changed from activerecord-3.0.0. (which
works well)
It used to be:
association.send(:add_record_to_target_with_callbacks, existing_record)
unless association.loaded?

The problem is:

_destroy option doesn’t work for nested attributes if the association is
not loaded.
I don’t think that’s the feature.

Here’s how I tested it in console.
(I intentionally removed some outputs for simplicity.)

Loading development environment (Rails 3.0.3)
ruby-1.8.7-p302 > post=Post.find 204
ruby-1.8.7-p302 > post.attachments.loaded?
=> false
ruby-1.8.7-p302 >
post.attributes={“attachments_attributes”=>{“0”=>{“id”=>“44”,
“_destroy”=>“1”}}}
ruby-1.8.7-p302 > post.save
=> true
ruby-1.8.7-p302 > post.attachments.map { |i| i.id }.join(", ")
=> “44, 45, 46, 47, 48, 49” #44 is still there.
ruby-1.8.7-p302 >
post.attributes={“attachments_attributes”=>{“0”=>{“id”=>“44”,
“_destroy”=>“1”}}}

Now I try to destroy it again when the attachments are loaded.

ruby-1.8.7-p302 > post.save
=> true
ruby-1.8.7-p302 > post.attachments.reload

To make sure that attachments are reloaded after the change.

ruby-1.8.7-p302 > post.attachments.map { |i| i.id }.join(", ")
=> “45, 46, 47, 48, 49” # Now 44 is gone.

Can anybody confirm that this is a bug?

Thanks.

Sam

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs