Невозможно загрузить полиморфную ассоциацию: item - PullRequest
0 голосов
/ 24 мая 2019

Мне нужно получить все учетные записи @client (has_many :accounts, as: :item - это отношения). Иметь учетную модель с: полиморфным полем элемента (belongs_to :item, polymorphic: true, не более), моделью продажи с has_many :accounts и belongs_to :client.

Мой запрос: @accounts = Account.where(company: current_user.company_id).joins(:items).where({ items: { client_id: @client.id } })

и получите следующую ошибку: Cannot eagerly load the polymorphic association :item

class Sale < ApplicationRecord
  belongs_to :user
  belongs_to :company
  belongs_to :category, required: false
  belongs_to :client, required: false
  has_many :accounts, as: :item
end

class Client < ApplicationRecord
  belongs_to :user
  belongs_to :company
  belongs_to :category, required: false
  has_many :sales
end

class Account < ApplicationRecord
  belongs_to :item, polymorphic: true
  belongs_to :user
  belongs_to :company
end

Ожидается вывод объекта Account со всеми платежами @client.

1 Ответ

2 голосов
/ 24 мая 2019

Кроме того, это может помочь вам получить ожидаемые результаты, разбив его на два запроса: -

sales_ids_for_client = @client.sales.pluck(:id)
accounts_of_given_client = Account.where("item_type = ? AND item_id IN (?)", 'Sale', sales_ids_for_client)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...