Ruby on rails 如何通过一个查询访问两个多态关联?

Ruby on rails 如何通过一个查询访问两个多态关联?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,在我的Rails 6应用程序中,我有以下型号: class Account < ApplicationRecord has_many :carriers has_many :clients # Unfortunately, these two don't work together. I have to uncomment one of them to get the other one to work: has_many :people, :through =>

在我的Rails 6应用程序中,我有以下型号:

class Account < ApplicationRecord

  has_many :carriers
  has_many :clients

  # Unfortunately, these two don't work together. I have to uncomment one of them to get the other one to work:
  has_many :people, :through => :carriers # works but omits clients
  has_many :people, :through => :clients # works but omits carriers

end
类帐户:carriers工作,但忽略了客户
有很多:人,:通过=>:客户#工作,但忽略了运营商
结束

class-Carrier:可个性化
结束

类客户端:可个性化
结束

class-Persontrue
结束

如何在一次查询中访问帐户的
运营商
和客户

我很想做一些类似于
account.people
的事情来显示所有的帐户的人员,但还没有找到一种方法来实现这一点


如何做到这一点?

您不能对两个关联使用相同的方法名称,相反,您可以将其重命名为
carrier\u people
client\u people
,并将两者同时加载

class Account < ApplicationRecord

  has_many :carriers
  has_many :clients

  has_many :carrier_people, :through => :carriers, source: :people # works but omits clients
  has_many :client_people, :through => :clients, source: :people # works but omits carriers

end

有很多人,>{joins(:carriers,:clients)}“
为你工作吗?不,@benjessop,不幸的是它没有。我得到了这个错误:
ActiveRecord::ConfigurationError(无法将“Person”加入名为“carriers”的协会;也许你拼错了?)
谢谢。但我如何筛选关联?我尝试了类似于
帐户的方法。包括(:carrier\u people,:client\u people)。people.where(:first\u name=>“John”)
但它给了我一个错误:
NoMethodError(未定义的方法“people”)你可以这样做,Account.includes(:carrier\u people,:client\u people)。在哪里(开利人:{名字:“约翰”})或(账户。包括(:开利人,:客户人)。其中(客户人:{名字:“约翰”})哇,比我想象的要复杂!
class Client < ApplicationRecord
  
  belongs_to :account

  has_many :people, :as => :personalizable

end
class Person < ApplicationRecord

  belongs_to :personalizable, :polymorphic => true

end
class Account < ApplicationRecord

  has_many :carriers
  has_many :clients

  has_many :carrier_people, :through => :carriers, source: :people # works but omits clients
  has_many :client_people, :through => :clients, source: :people # works but omits carriers

end
Account.includes(:carrier_people, :client_people)