This project is archived and is in readonly mode.

#3900 ✓stale
PeterW

Bug in nested_attributes with 2.3.5?

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

(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

Comments and changes to this ticket

  • Eloy Duran

    Eloy Duran February 23rd, 2010 @ 09:53 AM

    • Assigned user changed from “Eloy Duran” to “José Valim”
  • José Valim

    José Valim February 23rd, 2010 @ 09:56 AM

    Could you please create a failing test case?

  • PeterW

    PeterW March 7th, 2010 @ 02:45 PM

    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

  • José Valim

    José Valim March 7th, 2010 @ 02:47 PM

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

  • PeterW

    PeterW March 7th, 2010 @ 03:26 PM

    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.

  • José Valim

    José Valim March 7th, 2010 @ 03:34 PM

    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!

  • Yehuda Katz (wycats)

    Yehuda Katz (wycats) March 27th, 2010 @ 10:52 AM

    • State changed from “new” to “incomplete”
  • José Valim

    José Valim July 19th, 2010 @ 02:24 PM

    • State changed from “incomplete” to “stale”

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

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages