I have two models. Office and Employee. Employee has office_id as foreign key. So which one 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 correct, because for me, it does not make sense to declare foreign_key
in has_many
. A co-worker disagrees, and believes that the first example is correct, with the foreign key being declared on both sides of the relationship. I have not found many references to this subject. So, can anyone tell me which one is correct and why?