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?

  1. Just Category { has_many Posts }
  2. Just Post { belongs_to Category }
  3. Both Category { has_many Posts } AND Post { belongs_to Category }
Était-ce utile?

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
scroll top