简体   繁体   English

Rails模型has_many:through关系

[英]Rails model has_many :through relationship

User , Car , Part , Tire . UserCarPartTire User has many Car s. User有很多Car Car has many Part s. Car有很多Part Part has many Tire s. Part有很多Tire

In user.rb, I have 在user.rb中,我有

has_many :tires, through: :cars

However, it seems like this is not a correct way of doing it since there is another model Part in the middle. 但是,这似乎不是正确的方法,因为中间还有另一个模型Part

How should I successfully do to make has_many relationships between User and Tire ? 如何成功建立UserTire之间的has_many关系?

You just need to add the connection in the middle. 您只需要在中间添加连接。

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

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

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