Reputation: 10358
Here is the rails 3 query in the app:
Order.where('id IN (?)',ContractItem.where(contract_id: @contract.id).select('contract_item_id'))
Here is the only record in contract_item table:
As shown in the record, the @contract.id
is equal to 1 and we expect ContractItem.where(contract_id: @contract.id).select('contract_item_id')
returns array [2].
So Order.where('id IN (?)',ContractItem.where(contract_id: @contract.id).select('contract_item_id'))
should return order#2
. However what we have got is nothing. We figure an empty array must be returned instead. Is there something wrong with the code above?
Upvotes: 0
Views: 1051
Reputation: 36860
You're getting back an array, but it's an array of ContractItem objects with a single column, not an array of numbers.
Try instead...
Order.where('id IN (?)',ContractItem.where(contract_id: @contract.id).pluck(:contract_item_id))
Upvotes: 2