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.

Bug in nested_attributes with 2.3.5?

#3900

(sorry, I initially published this by mistake in the Rails Guide section)

Hi guys,

I just upgraded to 2.3.5 and my nested models that worked fine with 2.3.2 don't work anymore. It seems to me something might be wrong with 2.3.5 implementation of nested models.

In my case I have a "4 levels" nested arrangement (d belongs to c that belongs to b etc). When I do (very basic):

object_a = Object_a.new()
object_b = Object_b.new()
object_a.object_bs << object_b

object_c1 = Object_c.new()
object_c2 = Object_c.new()

object_d1 = Object_d.new()
object_d2 = Object_d.new()

object_c1.object_ds << object_d1 << object_d2
object_c2.object_ds << object_d1 << object_d2

object_b.object_cs << object_c1 << object_c2
object_a.object_d = object_d1  # a also has a d.

object_a.save

What happens:

object_d1, a, b, c1 are saved. Then d1 is associated to c1 (join table), and d2 is saved and associated to c1.

BUT then it tries again to associate d1 with c1 (instead of first saving c2) which raises an exception. Everything happens as if it had "forgotten" to save c2?

The exact same code works fine with 2.3.2.

Thanks,
Pierre

Reported by PeterW · February 8th, 2010 @ 07:42 PM

State: stale
Milestone: none
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Eloy Duran
    Eloy Duran
    • Assigned user changed from Eloy Duran to José Valim

    February 23rd, 2010 @ 09:53 AM

  2. José Valim
    José Valim

    Could you please create a failing test case?

    February 23rd, 2010 @ 09:56 AM

  3. PeterW
    PeterW

    Hi José

    I apologize it took me a long time to get back to you.

    I have created a simple app that shows the problem (calling http://localhost:3000/test_nested_save for example). By simply changing the Rails version in environment.rb, you will see that it works fine with 2.3.2 but fails with 2.3.5.

    Pierre

    March 7th, 2010 @ 02:45 PM

  4. José Valim
    José Valim

    Thanks Peter. But could you please try to add a failing test case in Rails test suite?

    March 7th, 2010 @ 02:47 PM

  5. PeterW
    PeterW

    I amended test/unit/object_a_test.rb with a test. Thing is, to be honest, I am not sure that's what you asked for. I don't really know how to "test" this in more details since it does almost nothing: it just creates instances of models and then links them as per the schema. It is the "save" itself on the "most parent" object that fails (it tries to save twice the same thing in the DB - as per my initial post).

    Please let me know if you need something else.

    March 7th, 2010 @ 03:26 PM

  6. José Valim
    José Valim

    What I asked you is to clone Rails from Github source, run ActiveRecord test suite and add a new test (which fails) to ActiveRecord test suite. In a few steps:

    git clone git://github.com/rails/rails.git
    cd rails
    vim Gemfile # Open your Gemfile and comment both mysql and pg gems
    cd activerecord
    rake test_sqlite3
    

    In the last step, you will execute ActiveRecord test suite with sqlite3. At this point, all tests should pass. Then you can open up activerecord/test/cases/autosave_association_test.rb and add a new test, which reproduces the failure you reported.

    Can you please do it? If you need any help, feel free to ask!

    March 7th, 2010 @ 03:34 PM

  7. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to incomplete

    March 27th, 2010 @ 10:52 AM

  8. José Valim
    José Valim
    • State changed from incomplete to stale

    Marking as stale. Let me know if you have any news.

    July 19th, 2010 @ 02:24 PM