可能です。まず、お客様のViewedContractor
モデルのbelongs_to
関連のclass_name
オプションを指定し、その両方がUser
クラスを参照するようにする必要があります。次にUser
モデルでhas_many through:
の関係を指定できます。このような
何か作業をする必要があります:
# viewed_contractor.rb
class ViewedContractor < ActiveRecord::Base
belongs_to :contractor, class_name: 'User', foreign_key: :contractor_id
belongs_to :customer, class_name: 'User', foreign_key: :customer_id
end
# user.rb
class User < ActiveRecord::Base
has_many :viewed_contractors_as_contractor, class_name: 'ViewedContractor', foreign_key: :contractor_id
has_many :viewed_contractors_as_customer, class_name: 'ViewedContractor', foreign_key: :customer_id
has_many :visited_contractors, through: :viewed_contractors_as_customer, source: :contractor
has_many :visited_customers, through: :viewed_contractors_as_contractor, source: :customer
end
恐ろしい:)ありがとうございます! –