Reputation: 623
I have a really complex query which joins 4 or 5 tables, and to make it easier, I execute it in 2 steps:
First I obtain the ids using raw sql:
annex_ids = ActiveRecord::Base.connection.select_all(sql_str)
After that I want the actual objects, with some joins also:
@annexes = Annex.includes(:contract, { service_address: {street: :city} }, :billing_address, { contract: [:client] }, :packages).find(annex_ids)
But I get the following error message:
Couldn't find all Annexes with IDs ({"id"=>3}, {"id"=>4}) (found 0 results, but was looking for 2)
Can anyone help? Thanks!
Upvotes: 0
Views: 45
Reputation: 2575
Couldn't find all Annexes with IDs ({"id"=>3}, {"id"=>4})
this error as find query expecting only ids
but
annex_ids = ActiveRecord::Base.connection.select_all(sql_str)
returning
{"id"=>3}, {"id"=>4}
So get ids from that, and pass to find
annex_ids.collect!{|c| c.values}
Then pass annex_ids
to query
Upvotes: 1