0

그래프 노드 클래스에 대한 구현이 있는데,이를 비순환 그래프로 지정했습니다. 내가 부모 노드의 관계를 잡아하려고하면통과 연결에서 기본 키를 지정하는 방법

class Node 
    has_many :edges 

    has_many :parents, 
      :foreign_key => 'parent_id', 
      :through => :edge 

    has_many :parent_edges, 
      :foreign_key => 'child_id', 
      :class_name => 'Edge', 
      :dependent => :destroy 

    # similar for children 
end 

class Edge 
    belongs_to :parent, 
      :class_name => 'Node' 

    # similar for child 

    has_many :edge_properties 
end 

을 다음과 같이

연관성은 대략 있으며, 생성 된 쿼리는 여전히 기본 키 node_id를 사용합니다.

SELECT `nodes`.* 
FROM `nodes` 
     INNER JOIN `edges` 
       ON `nodes`.`id` = 
        `edges`.`parent_id` 
WHERE `edges`.`node_id` = 16    # where clause uses node_id, should be child_id 
ORDER BY `nodes`.`name` ASC 

Where 절에서 child_id을 사용하도록 쿼리를 변경하려면 어떻게해야합니까?

+0

당신이 넣어하려고 했습니까? 또한 : SQL을 생성하는 Rails 코드를 보여줄 수 있습니까? –

답변

0

올바른 솔루션 (또한 TIL : 다른 연결을 지정할 수 있습니다 옵션을 통해) parent` 라인 : belongs_to 가장자리`에 child_id` : foreign_key => '

has_many :parent_edges, 
      :foreign_key => 'child_id', 
      :class_name => 'Edge', 
      :dependent => :destroy 

has_many :parents, 
      :source => 'parent', 
      :through => :parent_edges # using another association 
관련 문제