ActiveRecord class association
-
23-03-2021 - |
문제
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 }
해결책
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.
제휴하지 않습니다 StackOverflow