-
Notifications
You must be signed in to change notification settings - Fork 55
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 do I save data to association models? #244
Comments
You can eager-fetch both the main association and the join-model: https://github.com/geddy/model/blob/master/test/integration/adapters/sql/eager_assn.js#L71 So your returned User would have both Family and FamiliesUser (I would have used something like Membership or even FamilyMembership that makes sense on its own, but whatever. :)) and you can then update and save whichever of them you want. |
Good point on the naming thing. I'll go with So I'm doing these steps:
Would be a nice feature to be able to skip the 5th step since we have the association model as well as the User by that point. But you answered my question so I'll close it, thank you! :) |
You have the associated Family model, but the join-model instance (the FamilyMembership) doesn't exist until you save the association (which happens in step 4). We don't return ids for associated items that get saved in a cascade that way (I don't even know what the API for that would look like), so you have to do a fetch after the save to get the newly created FamilyMembership instance. Hope that helps explain. |
I see. Kinda makes it weird since some things should be required in the database ( |
Oh, that's a very good point -- there's not a nice validation story for the creation of join-model instances, since they're created implicitly. I'm not sure what a good way to handle explicit creation of join-models would be, or if there even is one. Probably the best approach is what you're describing. |
I have a
User
model and aFamily
model that are connected by aFamiliesUser
model (using{through: "FamiliesUsers"}
) . I'm still new to SQL, but from what I read theFamiliesUser
model is where you would store meta data about that relationship. For example, in this case, myfamilies_users
table has apermissionsLevel
property so we can set permissions per user per family. I'm not really clear how I go about setting or really getting this data.When I create a user, and it also creates the family and the user/family relationship, I don't get an ID for the created
FamiliesUser
. Which means to set the value I have to:FamiliesUser.first()
lookup where I look for the user id that also has the family idpermissionsLevel
propertyThat seems like a lot of work for setting any data in through models. Do you have suggestions or is there an undocumented way of passing data to through models when creating them?
The text was updated successfully, but these errors were encountered: