ActiveRecord class association
-
23-03-2021 - |
Question
Let's pretend that we have two tables, Posts and Categories, that have a many to one relationship, one category to many posts. Which would be the best way to go about defining that in the class definitions?
- Just Category { has_many Posts }
- Just Post { belongs_to Category }
- Both Category { has_many Posts } AND Post { belongs_to Category }
La solution
As you want to be able to do something like myCategory->Posts
if you have a category, but also myPost->categories
, you want to define both relations.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow