Rails模型has_many:通过关系

时间:2016-07-20 18:31:19

标签: ruby-on-rails ruby

.mobile-header { position: fixed; top: 0; left: 0; width: 100%; height: 75px; background-color: rgba(0, 0, 0, 0.8); } .hamburger { position: absolute; top: 20px; right: 20px; } UserCarPartTire有很多User个。 Car有很多Car个。 Part有很多Part s。

在user.rb中,我有

Tire

然而,似乎这不是一种正确的方法,因为中间还有另一个模型has_many :tires, through: :cars

我应该如何成功地在PartUser之间建立has_many关系?

1 个答案:

答案 0 :(得分:2)

您只需要在中间添加连接。

# User.rb
has_many :cars
has_many :parts, through: :cars
has_many :tires, through: :parts