1
I have two models. Office and Employee. Employee has office_id as foreign key. So, which is correct?
class Office < ActiveRecord::Base
has_many :employees, foreign_key: 'office_id'
end
class Employee < ActiveRecord::Base
belongs_to :office, foreign_key: 'office_id'
end
Or
class Office < ActiveRecord::Base
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :office, foreign_key: 'office_id'
end
I think the second example is the correct one, because for me, it makes no sense to declare the foreign_key
in the has_many
. A co-worker disagrees, and believes that the first example is the correct one, and the foreign key must be stated on both sides of the relationship. I haven’t found many references to this subject. So, can anyone tell me which of the two is correct and why?