rails joins polymorphic association

对着背影说爱祢 提交于 2019-12-05 05:17:59

You can eager load both polymorphic associations by using includes:

Notification.where(whatever: "condition").includes(:notifiable)

Considering both Bill and Balance results match the query result, the include should preload both models in the query result. ie:

Notification.where(whatever: "condition").includes(:notifiable).map(&:notifiable)
# => [Bill, Balance, etc]
manohar

Since you already have the bill and balance associations declared, you Can join individual associations and Do a Union on them.

Something like

scope :billiables_for_account, ->(account) do
  union_scope(joins(:bill).where(bills: {account: account}),
      joins(:balance).where(bills: {account: account}))
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!