在DataMapper中如何与同一模型有两个多对多关系?
|
编辑:更新了问题,以显示我对注释中所建议的ѭ0的使用。
我有两个看起来像这样的模型:
class Comparison
include DataMapper::Resource
property :id, Serial
end
class Msrun
include DataMapper::Resource
property :id, Serial
property :name, String
end
比较来自比较两组Msruns。我以为我可以通过从Compare到Msrun的两个多对多关系来表示这一点,但是我对如何在DataMapper中做到这一点感到不寒而栗。我知道可以通过添加类似以下内容来建立多对多关系:
has n, :whatevers, :through => Resource
但是,这只会在两个模型之间建立多对多关系。我还尝试创建两个联接模型并手动指定关系,并为每个关系手动指定子项,如下所示:
# Join model for the comparison-msrun many-to-many relationship.
class First
include DataMapper::Resource
belongs_to :msrun, :key => true
belongs_to :comparison, :key => true
end
# Join model for the comparison-msrun many-to-many relationship.
class Second
include DataMapper::Resource
belongs_to :msrun, :key => true
belongs_to :comparison, :key => true
end
class Comparison
include DataMapper::Resource
property :id, Serial
has n, :firsts
has n, :msrun_firsts, \'Msrun\', :through => :firsts, :child_key => [:msrun_id]
has n, :seconds
has n, :msruns_seconds, \'Msrun\', :through => :seconds, :child_key => [:msrun_id]
end
class Msrun
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :firsts
has n, :comparison_firsts, \'Comparison\', :through => :firsts, :child_key => [:comparison_id]
has n, :seconds
has n, :comparison_seconds, \'Comparison\', :through => :seconds, :child_key => [:comparison_id]
end
运行“ 4”会导致以下错误:
rake aborted!
No relationships named msrun_firsts or msrun_first in First
我在这里做错了什么?我该如何工作?
没有找到相关结果
已邀请:
2 个回复
粳饶瓢部
方法,或作为第2个参数传递给
希望有帮助!
蓄荣糖些