我想设置多个has_many:通过并行关系。这里是我的2个标准和2个连接模型:Rails has_many:through - >如何设置关联以从多个连接模型中拉取?
User.rb
has_many :ownerships, dependent: :destroy
has_many :devices, through: :ownerships
has_many :bookings, dependent: :destroy
has_many :devices, through: :bookings
Ownership.rb
belongs_to :user, touch: true, counter_cache: :devices_count
belongs_to :device, touch: true
Booking.rb
belongs_to :user, touch: true, counter_cache: :bookings_count
belongs_to :device, touch: true, counter_cache: :bookings_count
Device.rb
has_many :ownerships, dependent: :destroy
has_many :users, through: :ownerships
has_many :bookings, dependent: :destroy
has_many :users, through: :bookings
预期该电流设置不工作,似乎是加盟模式之间的串扰。我希望连接模型能够独立并行(即用户可以拥有关系 - 所有权 - 设备无法预订)。我不在这里寻找嵌套的has_many:through关系。
当我更改设备的用户所有权似乎改变了预订的数量,反之亦然......我应该如何正确设置它的任何想法?
感谢您的回答,我认为它是在正确的轨道上。你确定foreign_keys正确吗?他们不应该分别为ownership_id和booking_id吗? –
没问题 - 不,我不确定他们是否正确:)我会看看! –
这只是一个快速修复;至于为一个关联提供多组数据 - 我需要了解这个 –