Skip to content
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

How to upsert associations? #127

Open
hrdwdmrbl opened this issue May 12, 2022 · 1 comment
Open

How to upsert associations? #127

hrdwdmrbl opened this issue May 12, 2022 · 1 comment

Comments

@hrdwdmrbl
Copy link

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?

@hrdwdmrbl
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant