가능합니다. 먼저 User
클래스를 참조하여 ViewedContractor
모델의 belongs_to
연결에 class_name
옵션을 지정해야합니다. 그런 다음 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
신난다 :) 감사합니다! –