Hint: Check foreign key column exists for belongs_to [OK]
Common Mistakes:
Confusing belongs_to with has_many
Assuming association name should be plural
Ignoring missing database column
5. You want to create a Rails app where each Review belongs to a Product, and each product can have many reviews. You also want to ensure deleting a product deletes its reviews automatically. Which setup is correct?
hard
A. In Review model: belongs_to :product, dependent: :destroy In Product model: has_many :reviews
B. In Review model: belongs_to :product In Product model: has_many :reviews, dependent: :destroy
C. In Review model: has_many :product In Product model: belongs_to :reviews, dependent: :destroy
D. In Review model: belongs_to :product In Product model: has_many :reviews, dependent: :nullify
Solution
Step 1: Assign belongs_to in child model
The Review model should have belongs_to :product to link each review to one product.
Step 2: Use has_many with dependent option in parent
The Product model should have has_many :reviews, dependent: :destroy to delete reviews when product is deleted.