One way `has_many` relationships

Are you able to have a has_many relationship where it's just one way? I.e. I have a User resource which can have many List resources but I don't every really care about List resources knowing which users it belongs to
6 Replies
ZachDaniel
ZachDaniel3y ago
You can do it, but typically you want the foreign key that gets created from having a belongs_to relationship back to the other resource You can define private? relationships if you want the relationship to be usable internally but not exposed anywhere
Bread
BreadOP3y ago
But what about if multiple users can own the same list?
ZachDaniel
ZachDaniel3y ago
oh, then you'd want a many_to_many relationship and then the foreign key lives on the join resource
Bread
BreadOP3y ago
Ahh gotcha. I saw that in the docs but for some reason it's only obvious when you say it lol So in the future I may want one user who is the "primary" owner of a List and other users who are contributors. Would I model that as a has_many/belongs_to for the List and primary User, and then a many_to_many/join resource for the contributors?
ZachDaniel
ZachDaniel3y ago
You could do that, but you could also have a join resource that has a primary? attribute We don't have has_one + through yet but at some point we will
Bread
BreadOP3y ago
Okay cool. Thanks!

Did you find this page helpful?