active_record_upsert
active_record_upsert copied to clipboard
How to upsert associations?
Example where there is a uniqueness constraint on Children's names
parent = Parent.new
parent.children.build(name: "Ken")
parent.save!
When the parent saves, it will auto-save the child. How can I upsert
the child?
A possible solution is to intercept the save
method
def save(*args)
upsert(*args)
end
This seems to work in my testing but it makes me very nervous!