2011-02-15 65 views
2

我对RoR相当陌生 - 所以请温柔:)在rails中设置多个多对多关系的最佳方式是什么?

我想设置一个具有两个多对多关系的环境。

我在想什么是:

class A 
    has_many :c 
    has_many :d 
    has_many :b, :through=>c 
    has_many :b, :through=>d 
end 

class B 
    has_many :c 
    has_many :d 
    has_many :a, :through=>c 
    has_many :a, :through=>d 
end 

class C 
    belongs_top :a 
    belongs_to :b 
end 

class D 
    belongs_top :a 
    belongs_to :b 
end 

从这一切我已阅读多方面的:通过协会在一个类中的一个属性将无法正常工作。这个设置的全部目的是让我可以轻松地调用数据,引用c和d--即@ a.c和@ a.d,以及@ b.c和@ b.d。

有什么想法?

在此先感谢。

达摩

回答

4

你可以有多个的has_many:through关联,但你只是需要给他们不同的名字:

class A 
    has_many :c 
    has_many :d 
    has_many :cb, :through=>c, :class_name => "B" 
    has_many :db, :through=>d, :class_name => "B 
end 
+0

感谢托比!这工作完美:) – 2011-02-15 21:33:27

相关问题