I am trying to do something similar to this: Rails order by association field, only with an associated table. I have three models:
class Policy
has_many :clients
end
class Client
belongs_to :policy
belongs_to :address
end
class Address
end
and want to create a report collection, which will contain Policies with associated clients and their addresses. Something similar to this:
@report = Policy.where(...)
@report.includes(:clients, :addresses).order('clients.address.state desc')
but, of course, there is no direct association between Policy
and Model
and I get missing FROM-clause entry for table "address"
. I really don't want to add Policy > Address
association as there are other models that have addresses and belong to policies.