Agile Web Development with Rails 6 - Iteration L2 Need to add has_many relationship to Orders (PDF p 292)

On page 292, we create a SupportRequest model that belongs_to Order

class SupportRequest < ApplicationRecord belongs_to :order, optional: true end 

We also need to add the has_many end of the relationship to orders or else you will get a foreign key error when you attempt to destroy an Order that has a SupportRequest associated with that order id.

class Order < ApplicationRecord has_many :line_items, dependent: :destroy # need this relationship defined or it will break orders#destroy # since the relationship is optional on the SupportRequest end, it should nullify has_many :support_requests, dependent: :nullify # rest of class end 
1 Like