我有一个图节点类的实现,我希望它具有作为有向非循环图的功能。
协会大致如下
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 :(得分:0)
正确的解决方案(也是TIL:通过选项可以指定另一个关联)
has_many :parent_edges,
:foreign_key => 'child_id',
:class_name => 'Edge',
:dependent => :destroy
has_many :parents,
:source => 'parent',
:through => :parent_edges # using another association