How can I write this find with AR?

All -

I have an array of values arr = [‘a’,‘b’,‘c’] and I want to find all
records such that :value is contained within the array. Something like:

Record.find(:all,:conditions=>{:value=>arr.include?})

Does AR support this, and if so what is the syntax?

Thanks,
Drew

Drew O. wrote:

All -

I have an array of values arr = [‘a’,‘b’,‘c’] and I want to find all
records such that :value is contained within the array. Something like:

Record.find(:all,:conditions=>{:value=>arr.include?})

Does AR support this, and if so what is the syntax?

Thanks,
Drew

I’m out of office so cant really test but you could probably do
something along the lines of:
Record.find(:all,:conditions=>{ “value IN (?), arr.join(”,") })

Ben wrote:

I’m out of office so cant really test but you could probably do
something along the lines of:
Record.find(:all,:conditions=>{ “value IN (?), arr.join(”,") })

Thanks for putting me on the right track. It seems I can do:

Record.find(:all,:conditions=>[“value IN (?)”, arr])

you can also do

Model.find_all_by_value([‘a’, ‘b’, ‘c’])

ex:

authors = [‘Hemingway’, ‘Dickinson’, ‘Shakespeare’]
Books.find_all_by_author(authors)

where ‘author’ is a column in the ‘books’ table

You can simply do
Record.find(:all, :conditions => {:value => arr})

Simon

On Feb 21, 2007, at 16:07 , Drew O. wrote:

I have an array of values arr = [‘a’,‘b’,‘c’] and I want to find all
records such that :value is contained within the array. Something
like:

Record.find(:all,:conditions=>{:value=>arr.include?})

Does AR support this, and if so what is the syntax?

Does :conditions => [‘value IN (?)’, arr] do what you need?


Jakob S. - http://mentalized.net

Chris H. wrote:

you can also do

Model.find_all_by_value([‘a’, ‘b’, ‘c’])

Wow, much cleaner. Thanks for that!

-Drew