This project is archived and is in readonly mode.

#6204 ✓committed
Ryan Michael

accepts_nested_attributes_for fails if nested attributes set from Class finders

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

The following doesn't update the associated objects:


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

class Bar < ActiveRecord::Base
    belongs_to :foo

    # baz:string

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

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

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

f = Foo.first
f.bars_attributes = [{: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.

Comments and changes to this ticket

  • Alexey Nayden

    Alexey Nayden January 12th, 2011 @ 09:28 PM

    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.

  • Alexey Nayden

    Alexey Nayden January 12th, 2011 @ 09:28 PM

    • no changes were found...
  • Alexey Nayden

    Alexey Nayden January 12th, 2011 @ 09:29 PM

    • Assigned user set to “Santiago Pastorino”
  • Alexey Nayden

    Alexey Nayden January 12th, 2011 @ 09:38 PM

    • Tag changed from accepts_nested_attributes_for, association, caching, first to accepts_nested_attributes_for, association, caching, first, patch
  • Aaron Patterson

    Aaron Patterson January 12th, 2011 @ 11:47 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “Low”

    @Alexey your patch makes the AR tests fail with:

      1) Error:
    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?

  • Alexey Nayden

    Alexey Nayden January 13th, 2011 @ 01:07 AM

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

  • Alexey Nayden
  • Aaron Patterson

    Aaron Patterson January 28th, 2011 @ 11:14 PM

    • State changed from “open” to “committed”

    Merged and pushed. Thanks!

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>