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.

Updates to relationship resources are lost

#4830

Updates to a line_item resource which relates a product to a cart are lost.

Broken by commit 0265c708b9696c3943518ad5f3dabdc22c5eba11

testcase, output, git bisect

Reported by Sam Ruby · June 11th, 2010 @ 04:04 PM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: High

Activity

  1. Repository
    Repository

    (from [85cc1fa657f441417f36998a32a6a158c2697aad]) Revert "Don't overwrite unsaved updates when loading an association but preserve the order of the loaded records. [#4642 Unsaved changes to nested attribute collections are lost when using collection state:open]"

    This commit introduced a regression described in ticket [#4830 Updates to relationship resources are lost].

    This reverts commit 0265c708b9696c3943518ad5f3dabdc22c5eba11.
    http://github.com/rails/rails/commit/85cc1fa657f441417f36998a32a6a1...

    June 11th, 2010 @ 04:07 PM

  2. James Le Cuirot
    James Le Cuirot

    Hmm sorry about that. I was trying to fix another bug. I'll look into it as soon as I can.

    June 11th, 2010 @ 04:15 PM

  3. James Le Cuirot
    James Le Cuirot

    Figured it out and have a fix. I'll post it with an AR testcase shortly.

    June 12th, 2010 @ 10:35 AM

  4. James Le Cuirot
    James Le Cuirot

    The updates were not lost entirely as they were still saved to the database. They just weren't reflected in the model immediately afterwards. By keeping my previous fix but allowing already-saved records to be refreshed, everything is okay.

    The first attachment is against 2-3-stable where my original commit was not reverted.

    The second attachment is against master and reapplies the original commit along with the new fix. I have not got Rails 3 going yet and don't have time to do that today (had trouble when I last tried) so please run the tests to ensure it works.

    June 12th, 2010 @ 12:03 PM

  5. José Valim
    José Valim
    • Milestone cleared.
    • Assigned user set to José Valim

    June 17th, 2010 @ 04:34 PM

  6. Repository
    Repository
    • State changed from open to resolved

    (from [f664c57fe854c230d25c448b941569b81860fa79]) Don't overwrite unsaved updates when loading an association but preserve the order of the loaded records. Reapplied from before but now allows already-saved records to be refreshed.

    [#4830 Updates to relationship resources are lost state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/f664c57fe854c230d25c448b941569...

    June 19th, 2010 @ 11:33 PM

  7. helg
    helg
    • Importance changed from to High

    I'm still loosing _destroy attribute after accessing to collection

    June 30th, 2010 @ 11:02 AM

  8. James Le Cuirot
    James Le Cuirot

    I'm having a crisis this morning but I'll try and look into this tonight.

    June 30th, 2010 @ 11:09 AM

  9. szetobo
    szetobo

    By looking into the test case, the protected function :load_target must be called using send(:load_target) in order to retrieve the association collection together with the changes made by nested attributes assignment. Is it a desirable way to interact with Rails framework?

    Since I need to re-display the record (with nested attributes) on the input form upon validation error, I can only calling send(:load_target) all around in order to prevent the association collection reload from db and wipe out any changes. My problem case described here #5053 child/grandchild associations reload from db after nested attributes assignment.

    July 8th, 2010 @ 08:13 AM

  10. James Le Cuirot
    James Le Cuirot

    You don't have to call send(:load_target). I think we only do that in the tests because we need to call find on the array itself, not ActiveRecord's find. If we were to do target.find instead, it wouldn't actually load the collection.

    I haven't looked at your particular case but it sounds like the same problem I have already fixed. It should work fine with master or 2-3-stable now.

    July 8th, 2010 @ 09:22 AM

  11. szetobo
    szetobo

    We have a form to edit master-child-grandchild record at the same time using nested_attributes. When the form submit, I call master.update_attributes(params[:master]) and the validation callback is invoked. During the validation handler, I must access the submit data instead of data reload from db and that's why I need to access the association array instead of ActiveRecord's find from db.

    Also, if validation failed, I need to re-display the whole form together with error message and with error field highlight. Doing so also need to access the association array instead of refresh data from db.

    July 8th, 2010 @ 10:18 AM

  12. Jeremy Kemper