简体   繁体   中英

Rails model has_many :through relationship

User , Car , Part , Tire . User has many Car s. Car has many Part s. Part has many Tire s.

In user.rb, I have

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.

How should I successfully do to make has_many relationships between User and Tire ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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