I have two models ObjectA and ObjectB. ObjectB has two columns, a_1_id and a_2_id, which are both foreign keys to ObjectA. ObjectB belongs to each of these foreign ObjectA objects.
class ObjectA < ActiveRecord::Base
attr_accessible :player_1, :player_2, :subject, :turn
belongs_to :player_1, :class_name => "User"
belongs_to :player_2, :class_name => "User"
has_many :object_b, dependent: :destroy, :finder_sql => "SELECT * FROM object_bs where (a_1_id = #{id} or a_2_id = #{id})"
end
class ObjectB < ActiveRecord::Base
attr_accessible :a_1_id, :a_2_id
belongs_to :a_1_id, :class_name => "ObjectA"
belongs_to :a_2_id, :class_name => "ObjectA"
end
Is this the best way to setup the association?
Use the :foreign_key option: