Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

accepts_nested_attributes_for fails if nested attributes set from Class finders

#6204

The following doesn't update the associated objects:

Example

class Foo < ActiveRecord::Base
    has_many :bars
    accepts_nested_attributes_for :bars
end

class Bar < ActiveRecord::Base
    belongs_to :foo

    # baz:string
end

foo = Foo.create
bar = Bar.create(:foo => foo)

# Foo.first.bars.first => foo
# foo.bars.first => foo

Foo.first.bars_attributes = [{:id => @bar.id, :baz => 'fail!'}]
# Foo.first.bars.first == nil        -- possibly as intended

f = Foo.first
f.bars_attributes = [{:id => @bar.id, :baz => 'fail!'}]
# f.bars.first.baz == nil                -- probably not intended
# f.bars[0].baz == 'fail!'               -- works if offset used rather than first/last

It looks like bars.first and bars[0] are accessing different in-memory versions of the instance.

Reported by Ryan Michael · December 21st, 2010 @ 11:00 PM

State: committed
Milestone: none
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Alexey Nayden
    Alexey Nayden

    I can confirm that bug. That issue comes from #first behaviour which either loads first item from a database (when applicable) or loads the whole collection and merges it with in-memory changes. On the same time #[] method always uses the second way.

    Here're a failing test and a patch to fix this issue and to make #[] and #first behave consistently.

    January 12th, 2011 @ 09:28 PM

  2. Alexey Nayden
    Alexey Nayden
    • No changes were found…

    January 12th, 2011 @ 09:28 PM

  3. Alexey Nayden
    Alexey Nayden
    • Assigned user set to Santiago Pastorino

    January 12th, 2011 @ 09:29 PM

  4. Alexey Nayden
    Alexey Nayden
    • Tag changed from accepts_nested_attributes_for, association, caching, first to accepts_nested_attributes_for, association, caching, first, patch

    January 12th, 2011 @ 09:38 PM

  5. Aaron Patterson
    Aaron Patterson
    • State changed from new to open
    • Importance changed from to Low

    @Alexey your patch makes the AR tests fail with:

      1) Error:
    test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record(TestNestedAttributesInGeneral):
    ActiveRecord::RecordNotFound: Couldn't find Interest with ID=1059641603 for Man with ID=970345986
        /Users/apatterson/git/rails/activerecord/lib/active_record/nested_attributes.rb:457:in `raise_nested_attributes_record_not_found'
        /Users/apatterson/git/rails/activerecord/lib/active_record/nested_attributes.rb:423:in `block in assign_nested_attributes_for_collection_association'
        /Users/apatterson/git/rails/activerecord/lib/active_record/nested_attributes.rb:399:in `each'
        /Users/apatterson/git/rails/activerecord/lib/active_record/nested_attributes.rb:399:in `assign_nested_attributes_for_collection_association'
        /Users/apatterson/git/rails/activerecord/lib/active_record/nested_attributes.rb:287:in `interests_attributes='
        test/cases/nested_attributes_test.rb:156:in `test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record'
        /Users/apatterson/.local/lib/ruby/gems/1.9.1/gems/mocha-0.9.10/lib/mocha/integration/mini_test/version_142_and_above.rb:27:in `run'
        /Users/apatterson/git/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run'
        /Users/apatterson/git/rails/activesupport/lib/active_support/callbacks.rb:411:in `_run_setup_callbacks'
        /Users/apatterson/git/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
    

    Can you fix this and update the patch?

    January 12th, 2011 @ 11:47 PM

  6. Alexey Nayden
    Alexey Nayden

    @Aaron my bad. Patch fixed to work fine now. Sorry about that

    January 13th, 2011 @ 01:07 AM

  7. Alexey Nayden
  8. Aaron Patterson
    Aaron Patterson
    • State changed from open to committed

    Merged and pushed. Thanks!

    January 28th, 2011 @ 11:14 PM