Forum: Ruby on Rails Problems with find(:all, :conditions)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cassiano R. (Guest)
on 2007-04-18 17:20
(Received via mailing list)
I need find all people with "type" in 2,3,4,5...
so I wrote this find:

Person.find(:all, :conditions => ['type in (?)', "#{params[:types]}"]

and the params[:types] is an Array.

ActiveRecord generate this SQL:

select .... where type in ("12345")

But, I need this SQL instruction:

select .... where type in ("1", "2", "3", "4", "5" )

What is wrong?

thanks for your help.
Rob B. (Guest)
on 2007-04-18 17:48
(Received via mailing list)
On Apr 18, 2007, at 9:19 AM, Cassiano R. wrote:
>
> But, I need this SQL instruction:
>
> select .... where type in ("1", "2", "3", "4", "5" )
>
> What is wrong?
>
> thanks for your help.

Person.find(:all, :conditions => ['type in (?)', params[:types]])

If params[:types] is an Array, then don't force it to be a string by
interpolating it yourself with "#{}".

-Rob

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.