【发布时间】:2020-08-17 08:27:58
【问题描述】:
在我的 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
class Carrier < ApplicationRecord
belongs_to :account
has_many :people, :as => :personalizable
end
class Client < ApplicationRecord
belongs_to :account
has_many :people, :as => :personalizable
end
class Person < ApplicationRecord
belongs_to :personalizable, :polymorphic => true
end
如何在一个查询中访问帐户的carriers 和 clients?
我很想做account.people 之类的操作来向所有该帐户的人展示,但还没有找到实现这一目标的方法。
怎么做?
【问题讨论】:
-
has_many :people, -> { joins(:carriers, :clients) }"'适合你吗? -
不,@benjessop,不幸的是它没有。我收到此错误:
ActiveRecord::ConfigurationError (Can't join 'Person' to association named 'carriers'; perhaps you misspelled it?)
标签: ruby-on-rails ruby activerecord