简体   繁体   English

在Rails中建立多个多对多关系的最佳方法是什么?

[英]What is the best way to setup multiple many-to-many relationships in rails?

I am fairly new to RoR - so please be gentle :) 我对RoR还是很陌生-所以请保持柔和:)

i am trying to setup an environment that has two many to many relationships. 我正在尝试建立一个具有两个多对多关系的环境。

What I am thinking is: 我在想的是:

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

From all this I have read multiple :through associations to the one attribute in the one class will not work. 从所有这些中,我读到了多个:through关联到一个类中的一个属性将不起作用。 And the whole purpose of this setup is so I can easily call data with reference to both c and d - ie @ac and @ad, as well as @bc and @bd 而且此设置的全部目的是让我可以轻松地同时引用c和d来调用数据-即@ac和@ad以及@bc和@bd

Any thoughts? 有什么想法吗?

Thanks in advance. 提前致谢。

Damo 达摩

You can have multiple has_many :through associations, but you just need to give them different names: 您可以具有多个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM