user938363
user938363

Reputation: 10358

A select in rails query does not return an array

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:

enter image description here

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

Answers (1)

SteveTurczyn
SteveTurczyn

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

Related Questions