Find Multiple Conditions

Hi,

I'm getting my records using:

@users= User.find(:all,:conditions=>["country=? ",country])

where country is a String with the name of the country.

But now I want to get the result for different countries.
I have an array
countries = ['Germany','France','Spain']

and I want to get all the records where country is one of those
included in the array

How can I do that?

Thanks

cfingerh wrote:

But now I want to get the result for different countries.
I have an array
countries = ['Germany','France','Spain']

and I want to get all the records where country is one of those
included in the array

The following suggestion is a detestable hack, but it works. The names of countries (someone correct my logic here) don't contain single or double quotes, right? That means my hack will work, regardless how a database escapes quotes within strings:

:conditions=> "country in (#{ countries.map(&:inspect).join(',') }) "

The &:inspect is a trick to put quotes around each country's name.

And no post advising SQL abuse would be complete without warning against SQL-insertion attacks. .inspect makes those impossible too, but at the cost of very probably crashing your query.

Remember that ActiveRecord does Ruby things better than SQL things at times, meaning to get the most out of your queries you ought to learn more SQL SELECT statements. And there might also be a better Rails way to do what I did.