README Clarification for has_one#247
Conversation
A small change to save others some troubleshooting time. For reasons too complicated to explain here I need to allow users to delete and create a record with a 'has_one' association. The destruction of the associated object is expected as per has_one#association= documentation (excerpt below). Assigns the associate object, extracts the primary key, sets it as the foreign key, and saves the associate object. To avoid database inconsistencies, permanently deletes an existing associated object when assigning a new one, even if the new one isn't saved to database.
|
Are you saying the currently linked object is destroyed from the moment the view is rendered? (so when the There is a simple work-around: use the :force_non_association_create option. This feels like something that used to work in rails 3, and now no longer does. I am not sure if it is a bug in rails, as soon as the So the current work-around is specifying that option manually, I will try to make sure I fix the code. It is not as correct to manually build the object (the association could have options which are not taken into account when manually building the object), but less worse than deleting the current object immediately 😄 |
|
Agreed: It was an interesting bug to chase down because I was seeing the field set rendered twice. But once I looked through the gem code it all made sense. Rendered journal/set_groups/_intensity_fields.html.erb (0.9ms) # Expected to render once.
(0.1ms) BEGIN
SQL (0.2ms) DELETE FROM "intensities" WHERE "intensities"."id" = $1 [["id", 12026]]
(7.9ms) COMMIT
Rendered journal/set_groups/_intensity_fields.html.erb (0.8ms) # Rendered during link_to_add_associationI think this is another argument against using |
|
Here's some console confirmation of the behaviour: e = ExerciseSet.last #<ExerciseSet id: 12040...
intensity1 = e.intensity #<Intensity id: 12055...
e.build_intensity #<Intensity id: nil...
e2 = ExerciseSet.last #<ExerciseSet id: 12040...
e2.intensity #nil
Intensity.find(12055) #ActiveRecord::RecordNotFound: Couldn't find Intensity with 'id'=12055 |
A small change to the README save others some troubleshooting time. For reasons too complicated to explain here I need to allow users to delete and create a record with a 'has_one' association.
The destruction of the associated object is expected as per has_one#association= documentation (excerpted):
Assigns the associate object, extracts the primary key, sets it as the foreign key, and saves the associate object. To avoid database inconsistencies, permanently deletes an existing associated object when assigning a new one, even if the new one isn't saved to database.