Ruby on rails Rails有很多:通过-->;如何设置关联以从多个联接模型中提取?

Ruby on rails Rails有很多:通过-->;如何设置关联以从多个联接模型中提取?,ruby-on-rails,ruby-on-rails-4,has-many-through,Ruby On Rails,Ruby On Rails 4,Has Many Through,我想通过并行关系建立多个has\u many。以下是我的2个标准和2个连接模型: User.rb has_many :ownerships, dependent: :destroy has_many :devices, through: :ownerships has_many :bookings, dependent: :destroy has_many :devices, through: :bookings belongs_to :user, touch: true, counter_

我想通过并行关系建立多个has\u many。以下是我的2个标准和2个连接模型:

User.rb

has_many :ownerships, dependent: :destroy
has_many :devices, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :devices, through: :bookings
belongs_to :user, touch: true, counter_cache: :devices_count
belongs_to :device, touch: true
belongs_to :user, touch: true, counter_cache: :bookings_count 
belongs_to :device, touch: true, counter_cache: :bookings_count
has_many :ownerships, dependent: :destroy
has_many :users, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :users, through: :bookings
has_many :ownerships, dependent: :destroy
has_many :device_ownerships, through: :ownerships, class_name: "Device", foreign_key: "device_id", source: :device

has_many :bookings, dependent: :destroy
has_many :device_bookings, through: :bookings, class_name: "Device", foreign_key: "device_id", source: :device
has_many :ownerships, dependent: :destroy
has_many :user_ownerships, through: :ownerships, class_name: "User", foreign_key: "user_id", source: :user

has_many :bookings, dependent: :destroy
has_many :user_bookings, through: :bookings, class_name: "User", foreign_key: "user_id", source: :user
所有权.rb

has_many :ownerships, dependent: :destroy
has_many :devices, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :devices, through: :bookings
belongs_to :user, touch: true, counter_cache: :devices_count
belongs_to :device, touch: true
belongs_to :user, touch: true, counter_cache: :bookings_count 
belongs_to :device, touch: true, counter_cache: :bookings_count
has_many :ownerships, dependent: :destroy
has_many :users, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :users, through: :bookings
has_many :ownerships, dependent: :destroy
has_many :device_ownerships, through: :ownerships, class_name: "Device", foreign_key: "device_id", source: :device

has_many :bookings, dependent: :destroy
has_many :device_bookings, through: :bookings, class_name: "Device", foreign_key: "device_id", source: :device
has_many :ownerships, dependent: :destroy
has_many :user_ownerships, through: :ownerships, class_name: "User", foreign_key: "user_id", source: :user

has_many :bookings, dependent: :destroy
has_many :user_bookings, through: :bookings, class_name: "User", foreign_key: "user_id", source: :user
Booking.rb

has_many :ownerships, dependent: :destroy
has_many :devices, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :devices, through: :bookings
belongs_to :user, touch: true, counter_cache: :devices_count
belongs_to :device, touch: true
belongs_to :user, touch: true, counter_cache: :bookings_count 
belongs_to :device, touch: true, counter_cache: :bookings_count
has_many :ownerships, dependent: :destroy
has_many :users, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :users, through: :bookings
has_many :ownerships, dependent: :destroy
has_many :device_ownerships, through: :ownerships, class_name: "Device", foreign_key: "device_id", source: :device

has_many :bookings, dependent: :destroy
has_many :device_bookings, through: :bookings, class_name: "Device", foreign_key: "device_id", source: :device
has_many :ownerships, dependent: :destroy
has_many :user_ownerships, through: :ownerships, class_name: "User", foreign_key: "user_id", source: :user

has_many :bookings, dependent: :destroy
has_many :user_bookings, through: :bookings, class_name: "User", foreign_key: "user_id", source: :user
设备.rb

has_many :ownerships, dependent: :destroy
has_many :devices, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :devices, through: :bookings
belongs_to :user, touch: true, counter_cache: :devices_count
belongs_to :device, touch: true
belongs_to :user, touch: true, counter_cache: :bookings_count 
belongs_to :device, touch: true, counter_cache: :bookings_count
has_many :ownerships, dependent: :destroy
has_many :users, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :users, through: :bookings
has_many :ownerships, dependent: :destroy
has_many :device_ownerships, through: :ownerships, class_name: "Device", foreign_key: "device_id", source: :device

has_many :bookings, dependent: :destroy
has_many :device_bookings, through: :bookings, class_name: "Device", foreign_key: "device_id", source: :device
has_many :ownerships, dependent: :destroy
has_many :user_ownerships, through: :ownerships, class_name: "User", foreign_key: "user_id", source: :user

has_many :bookings, dependent: :destroy
has_many :user_bookings, through: :bookings, class_name: "User", foreign_key: "user_id", source: :user
当前设置未按预期工作,连接模型之间似乎存在串扰。我希望连接模型是独立的和并行的(即用户可以与设备建立关系(所有权),而不需要预订设备)。我不是在寻找一个嵌套的多:通过关系在这里


当我改变一台设备的用户所有权,这似乎改变了预订数量,反之亦然。。。关于如何正确设置,您有什么想法吗?

我认为您遇到的第一个错误是,您使用相同的名称调用了两个关联(
用户
/
设备

为了帮助更多的受访者,真正的问题是-->
如何设置关联以从多个加入模型中提取?


快速修复

Rails关联主要由它们的类命名,但是由于冲突,您应该避免设置它们两次。这就是你看到当前问题的原因。一个简单的解决方案是用不同的名称来称呼协会:

User.rb
has_many :ownerships, dependent: :destroy
has_many :owner_devices, through: :ownerships, class_name: "Device", foreign_key: "ownership_id"

has_many :bookings, dependent: :destroy
has_many :booking_devices, through: :ownerships, class_name: "Device", foreign_key: "booking_id"

我仍在寻找有关如何设置关联以使用两种模型的信息

根据Rich Peck的建议,这似乎是一个可行的解决方案:

User.rb

has_many :ownerships, dependent: :destroy
has_many :devices, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :devices, through: :bookings
belongs_to :user, touch: true, counter_cache: :devices_count
belongs_to :device, touch: true
belongs_to :user, touch: true, counter_cache: :bookings_count 
belongs_to :device, touch: true, counter_cache: :bookings_count
has_many :ownerships, dependent: :destroy
has_many :users, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :users, through: :bookings
has_many :ownerships, dependent: :destroy
has_many :device_ownerships, through: :ownerships, class_name: "Device", foreign_key: "device_id", source: :device

has_many :bookings, dependent: :destroy
has_many :device_bookings, through: :bookings, class_name: "Device", foreign_key: "device_id", source: :device
has_many :ownerships, dependent: :destroy
has_many :user_ownerships, through: :ownerships, class_name: "User", foreign_key: "user_id", source: :user

has_many :bookings, dependent: :destroy
has_many :user_bookings, through: :bookings, class_name: "User", foreign_key: "user_id", source: :user
Booking.rb(加入模式)

所有权.rb(加入模型)

设备.rb

has_many :ownerships, dependent: :destroy
has_many :devices, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :devices, through: :bookings
belongs_to :user, touch: true, counter_cache: :devices_count
belongs_to :device, touch: true
belongs_to :user, touch: true, counter_cache: :bookings_count 
belongs_to :device, touch: true, counter_cache: :bookings_count
has_many :ownerships, dependent: :destroy
has_many :users, through: :ownerships

has_many :bookings, dependent: :destroy
has_many :users, through: :bookings
has_many :ownerships, dependent: :destroy
has_many :device_ownerships, through: :ownerships, class_name: "Device", foreign_key: "device_id", source: :device

has_many :bookings, dependent: :destroy
has_many :device_bookings, through: :bookings, class_name: "Device", foreign_key: "device_id", source: :device
has_many :ownerships, dependent: :destroy
has_many :user_ownerships, through: :ownerships, class_name: "User", foreign_key: "user_id", source: :user

has_many :bookings, dependent: :destroy
has_many :user_bookings, through: :bookings, class_name: "User", foreign_key: "user_id", source: :user

老实说,我有点困惑为什么外键需要设置成现在的样子,所以我必须多读一点。否则,它似乎是功能性的,我再也看不到这些连接模型之间的串扰了。

感谢您的回答,我认为它走上了正确的轨道。您确定外键正确吗?它们不应该分别是所有权id和预订id吗?没问题-不,我不确定它们是否正确:)我会看一看!这只是一个快速解决方案;至于为一个关联提供多组数据,我需要了解这一点。检查我发布的答案,我将首先尝试并使用它。但我认为这是它的要点,肯定需要别名和类名称的引用。现在只需要找出正确的外国身份证就行了。如果有效,请告诉我。如果您遇到错误/问题,请告诉我&我们可以解决它。回答“深度”问题的一个问题是,我必须重新创建您的环境以评估响应等;我更愿意让询问者做一些事情,因为我们正在努力修复他们的设置!很好-如果您使用非常规关联,那么
外键基本上是一个定义正确列名的变量。最好将关联视为方法,它具有从名称派生的自动选项。如果您使用的是非标准名称,您的论点需要修改为sui t:)顺便说一句,如果我的答案有帮助,您可能希望对其进行更新投票;)