1

내 레일 앱에는 PersonCompany의 두 가지 모델이 있습니다. 이 객체의 모든 쌍 사이에 다 대다 관계를 지정해야합니다. 그래서 내가 할 수 있어야하는 것은 다음과 같이 수행 @connectionsPersonCompany 객체의 배열입니다이중 다형성 연관

@connections = @person.connections 

.

class ConnectionBinding < ActiveRecord::Base 
    belongs_to :connect_from, polymorphic: true 
    belongs_to :connect_to, polymorphic: true 
end 

ActiveRecord::HasManyThroughAssociationPolymorphicSourceError 예외

는 사람은 이미이 문제를 해결 하는가 thougths : 지금 나는이에 대한 ConnectionBinding 모델을 만들었으며이 같은 원 작동하지 않습니다

? 어떤 제안도 감사합니다.

답변

1

아이디어를 보여주는 Thanx Chris. 그러나 나는이 일을하기 위해 약간의 변화를 할 필요가있다. Chris 예제의 문제점은 person_connections 메서드를 호출하면 잘못된 쿼리가 생성된다는 것입니다. person_idconnect_from_id 컬럼을해야한다는

Person.find(720).person_connections 

이 SQL을

SELECT "people".* FROM "people" 
INNER JOIN "connection_bindings" 
    ON "people"."id" = "connection_bindings"."connect_to_id" 
WHERE "connection_bindings"."person_id" = 720 
    AND "connection_bindings"."connect_to_type" = 'Person' 

를 생성한다. 그래서 has_many :connection_bindings:as => :connect_from 옵션을 추가하여 ActiveRecord에 다형성 연관을 표시해야합니다.

has_many :connection_bindings, :as => :connect_from 
has_many :person_connections, :through => :connection_bindings, 
    :source => :connect_to, source_type: 'Person' 
has_many :company_connections, :through => :connection_bindings, 
    :source => :connect_to, source_type: 'Company' 

그러나 아직 충분하지 않습니다. 역 연결을 할 수 있어야합니다. 따라서 사람 @a이 사람 @b에 연결을 추가하면?메서드 연결은 양방향 @a.connections@b.connections의 연결을 나타내야합니다.

이제 추가 연결 및 집계 방법을 몇 개 추가하여이 작업을 수행 할 수 있다고 생각합니다.

5

ActiveRecord가 찾고있는 관련 열을 알려줘야합니다. 나는 다음과 같은 갖고 싶어 같은데요 :

class Person < ActiveRecord::Base 
    has_many :connection_bindings 
    has_many :companies, :through => :connection_bindings 
    has_many :people, :through => :connection_bindings 
end 

class company < ActiveRecord::Base 
    has_many :connection_bindings 
    has_many :companies, :through => :connection_bindings 
    has_many :people, :through => :connection_bindings 
end 

문제는 두 테이블이 하나 개의 컬럼에서이 ID를 넣고 레일이 조회하는 테이블 모른다 가지고있다.

예를 들어 데이터베이스의 모든 connection_binding 행에서 connect_from은 company_id 또는 person_id가 될 수 있으며 connect_to도 마찬가지입니다. 그래서 당신은 '안녕 레일, 내 연결된 ConnectionBindings'로드 및 connect_from 11 및 connect_to 12 행을 가져옵니다. 그러나 그것은 Person.find (12) 또는 Company.find (12)합니까? 말할 방법이 없습니다!

class Person < ActiveRecord::Base 
    has_many :connection_bindings 
    has_many :person_connections, :through => :connection_bindings, :source => :to_connect, :source_type => 'Person' 
    has_many :company_connections, :through => :connection_bindings, :source => :to_connect, :source_type => 'Company 

    def connections 
    person_connections + company_connections 
    end 
end 

당신은 다른 측면에서 그를 구축해야합니다 (물론 관련 : from_connect의)

대신 레일 좀 더 많은 정보를 제공해야하지만, 그 방법에 따라 달라집니다 이 연결을 사용하고 있습니다. 당신을 시작할 수있을만큼 충분해야합니다.

Ruby와 Rails의 마법의 세계에서 익숙한 것보다 훨씬 더 많은 타이핑을하지만, 매우 복잡한 데이터 패턴으로 구성하려고합니다. 그것이 불가능하다는 것을 의미하지는 않습니다. 레일스는 훌륭한 프레임 워크로서 여러분이 정말로 원하는 것을하지 못하게 할 것입니다. 그러나 여러분의 목적을 명확하게 설명하는 것은 드문 일입니다.

+0

고맙습니다. 대답은 매우 도움이되었습니다. 나는 다음 답에서 나를 위해 실제 해결책을 적었다. – RunFor

0

우려 :

module Linkable 
    extend ActiveSupport::Concern 

    included do 
    has_many :links_to, as: :linkable_to, class_name: 'Link' # [this]->[other object] 
    has_many :links_from, as: :linkable_from, class_name: 'Link' # [other object]->[this] 
    end 
end 

링크 테이블 : 저를 지적

class Link < ActiveRecord::Base 

    #The idea for this class is to by a double polymorphic table, linking an object to another object 
    belongs_to :linkable_from, :polymorphic => true 
    belongs_to :linkable_to, :polymorphic => true 
end