我有两个模型:人和关系.第二个存储有关两个人之间关系的信息.它有parent_id和child_id字段,并且没有id字段.我用has_many连接它:通过它可以工作.
但:
我的模型看起来像这样:
class Person < ActiveRecord::Base
has_many :child_relations,
:class_name => "Relation",
:foreign_key => "parent_id"
has_many :parent_relations,
:class_name => "Relation",
:foreign_key => "child_id"
has_many :children, :through => :child_relations
has_many :parents, :through => :parent_relations
end
class Relation < ActiveRecord::Base
belongs_to :parent, :class_name => "Person"
belongs_to :child, :class_name => "Person"
end
Run Code Online (Sandbox Code Playgroud)
有什么建议?
更新:我使用过has_many:通过 becouse我还存储有关表中关系类型的信息.目前我放弃了,我在我的表中添加了id字段(Rails约定......).但我的问题仍未解决.