dfg59
February 21, 2007, 4:07pm
1
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
dfg59
February 21, 2007, 4:11pm
2
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(”,") })
dfg59
February 21, 2007, 4:30pm
3
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])
dfg59
February 21, 2007, 4:57pm
4
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
dfg59
February 21, 2007, 11:24pm
5
You can simply do
Record.find(:all, :conditions => {:value => arr})
Simon
dfg59
February 22, 2007, 12:18am
6
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
dfg59
February 21, 2007, 5:47pm
7
Chris H. wrote:
you can also do
Model.find_all_by_value([‘a’, ‘b’, ‘c’])
Wow, much cleaner. Thanks for that!
-Drew