'Why is my nested `.update` command running a joins on the background?
I have a model:
class Brother < ActiveRecord
has_many :sisters, through: :brothers_sisters
has_many :brothers_sisters
accepts_nested_attributes_for :sisters
end
And I would like to add a new Sister id: 3 to my @brother. I believe! I could write something like this :
@brother.update(sister_attributes: [{id: 1}, {id: 2}, {id: 3}])
However, this runs on the background this SQL :
Brother Load (5.6ms) SELECT `brothers`.* FROM `brothers` INNER JOIN `brothers_sisters` ON `brother`.`id` = `brothers_sisters`.`brother_id` WHERE `brothers_sisters`.`sister_id` = 1 AND `brothers`.`id` IN (1, 2, 3)
But because of the INNER JOIN, and because id: 3 does not exist, then the whole .update fails.
However if I run this, it works exactly as expected :
@brother.update(sister_ids: [1,2,3])
The Question: How can I accomplish auto-generating the missing joins tables but with the nested attributes in my first failing example?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
