繁体   English   中英

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

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

我对RoR还是很陌生-所以请保持柔和:)

我正在尝试建立一个具有两个多对多关系的环境。

我在想的是:

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

从所有这些中,我读到了多个:through关联到一个类中的一个属性将不起作用。 而且此设置的全部目的是让我可以轻松地同时引用c和d来调用数据-即@ac和@ad以及@bc和@bd

有什么想法吗?

提前致谢。

达摩

您可以具有多个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