Wednesday, April 9, 2008

Behaviour of 'in' clause

I had this following code in one of my finder statements:
:conditions => ["subscription_id IN (?)",subscription_ids.join(",")]

That was producing the SQL statement:
SELECT * FROM memberships WHERE (subscription_id IN ('640,610,641,639,642,620,645'))

But that statement didn't yield any result set.

Whereas the following code:
:conditions => "subscription_id IN (#{subscription_ids.join(',')})"

..produces the SQL statement:
SELECT * FROM memberships WHERE (subscription_id IN (640,610,641,639,642,620,645))

which gave me the correct result set.

The difference between the 2 SQL statements is the usage of single-quotes around the numbers.

1 comment:

gil said...

Try the following instead:

:conditions => ["subscription_id IN (?)",subscription_ids]