-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rendering the edit screen for an item with a has_one association deletes the association #609
Comments
note this also breaks updating, because by the time we actually try to save an update, the bar has already been deleted, so the association fails to find the bar with the expected id to update |
Yes this is a known issue, because we are using the association details to create the nested items, rails presumes, for a This was raised in #414 and answered on stackoverflow. In short you can easily circumvent this by using the
which will create a new nested element, but not using the association, so not interfering with the current state in the database. This specific case is also documented in the wiki but is afaik rarely used (since there is less of a dynamic aspect managing a single child, as opposed to adding an unlimited number of childs I guess). |
thanks for the super-quick response. My google-foo is clearly not up to scratch! would you accept a pull request to add a short note to this effect in the main page? |
Sure 👍 |
Ha there already was some effort to document it (or warn of this behaviour) in #247, and in that very very very old PR I claim I would attempt to fix the code. Looking at the code I can see two things:
Since I have to explicitly decide to call the I will brew a fix 👍 |
Foo has_one Bar (optional)
Creating a foo with a Bar works correctly
However -merely showing the edit form causes the associated bar to be deleted
steps:
create Foo with nested Bar
this shows http://localhost:3000/foos/2 where we can see that foo1 does indeed have a bar
Click edit
http://localhost:3000/foos/2/edit
this shows the foo, and the associated bar
however: The Bar has already been deleted even though we have never directly called save, or clicked on the save button)
(opening http://localhost:3000/foos/2 agin confirms this)
the line which causes this problem is
link_to_add_association 'add Bar', f, :bar
The wild thing to me is the idea that simply rendering link_to_add_association could change my model (by deleting an association)
demo project here:
https://github.com/ConfusedVorlon/Cocoon-AddAssociationDelete-Example
key classes:
The text was updated successfully, but these errors were encountered: