Retrieving Mutliple Records using find_by_*

I was hoping that AR's find_by_* magic methods would help me retrieve multiple items somewhat like the following:

  names_to_find = ['Larry', 'Moe', 'Curly']   found_people = Person.find_by_name(names_to_find)

Is there a way to accomplish this using find_by_* (or even find(:all) using some sort of conditions) or must I resort to SQL (of which I happily know very little).

Thanks, Chris

Use Person.find_all_by_name(names_to_find). :slight_smile:

Craig

I was hoping that AR's find_by_* magic methods would help me retrieve multiple items somewhat like the following:

names_to_find = ['Larry', 'Moe', 'Curly'] found_people = Person.find_by_name(names_to_find)

find_by_xxx is mapped to a find :first, its counterpart is find_all_by...

Were you to write it out in full you'd write Person.find :all, :conditions => ["name in (?)", names] or Person.find :all, :conditions => {:name => names}

Fred

Hi everyone i'm new to ROR.

I did try before the method Person.find :all, :conditions => ["name in (?)", names] or Person.find :all, :conditions => {:name => names} but its gave me a error uninitialized constant Person. It is must implement a method for Person in order to use it. Thank

Wawa

Hi Emmie,

Emmie Wawa wrote:

Hi everyone i'm new to ROR.

Welcome!

I did try before the method Person.find :all, :conditions => ["name in (?)", names] or Person.find :all, :conditions => {:name => names} but its gave me a error uninitialized constant Person.

The error is telling you that Rails can't find a model named Person.rb. Do you have one? And a table named persons?

Bill

Wouldn’t the table be named “people”? :slight_smile:

Inflector.pluralize(“person”) => “people”

Oops! Yes it would :-p Thanks for the catch, Ryan!

Best regards,

Bill

Hi Bill,

Ok thanks I have solved the error uninitialized constant Person but now i have another error showing me that i have a nil object when you didn't expect it! (NoMethodError). The error occurred while evaluating nil.find :all, :conditions => {:name => names}

Thanks

wawa

Hi Emmie,

Emmie Wawa wrote:

Ok thanks I have solved the error uninitialized constant Person but now i have another error showing me that i have a nil object when you didn't expect it! (NoMethodError). The error occurred while evaluating nil.find :all, :conditions => {:name => names}

You'll need to show us some code to help further. How exactly did you solve the unintialized constant error?

Best regards, Bill