# Searching database using foriegn keys

**URL:** <https://www.ruby-forum.com/t/searching-database-using-foriegn-keys/54618>\
**Category:** Rails\
**Created:** [March 10, 2006, 4:05am UTC](https://www.ruby-forum.com/t/searching-database-using-foriegn-keys/54618 "2006-03-10T04:05:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [March 10, 2006, 4:05am UTC](https://www.ruby-forum.com/t/searching-database-using-foriegn-keys/54618/1 "2006-03-10T04:05:43Z")

</div>

I’ve read a lot about the ORM magic of Active Record which allows you to  
address a foriegn key object directly…

e.g.

I have a books and people table where books has a column called  
person\_id  
If I get myself a Book object called mybook I get to the person object  
associated with this book by saying

book.person

which is awesome and I love it.

My question, how can I say "Go and get me all the books whose person =  
“Andy Griffith”. See what I mean? I want to search for a bunch of book  
rows based on some value in the people table. How can I do that?  
Thanks.

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [March 10, 2006, 5:26am UTC](https://www.ruby-forum.com/t/searching-database-using-foriegn-keys/54618/2 "2006-03-10T05:26:42Z")

</div>

On Mar 9, 2006, at 7:05 PM, John R. wrote:

> rows based on some value in the people table. How can I do that?  
> person.books

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [March 10, 2006, 9:53am UTC](https://www.ruby-forum.com/t/searching-database-using-foriegn-keys/54618/3 "2006-03-10T09:53:17Z")

</div>

Hi John,

> My question, how can I say "Go and get me all the books whose person =  
> “Andy Griffith”. See what I mean? I want to search for a bunch of book  
> rows based on some value in the people table. How can I do that?  
> Thanks.

Try:

Person.find\_by\_name(“Andy Griffith”).books

This assumes that your people database table has a column called “name”,  
otherwise simple replace name by whatever your column is called (e.g  
Person.find\_by\_fullname).  
Or if you did split firstname/lastname in your database

Person.find\_by\_firstname\_and\_lastname(‘Andy’, ‘Griffith’).books

Matt

---

<div class="post-metadata">

**Author:** ![jjrussell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8baadc/32.png) [@jjrussell](https://www.ruby-forum.com/u/jjrussell)\
**Post date:** [March 10, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/searching-database-using-foriegn-keys/54618/4 "2006-03-10T16:04:41Z")

</div>

Matthias von Rohr wrote:

> Hi John,
> 
> > My question, how can I say "Go and get me all the books whose person =  
> > “Andy Griffith”. See what I mean? I want to search for a bunch of book  
> > rows based on some value in the people table. How can I do that?  
> > Thanks.
> 
> Try:
> 
> Person.find\_by\_name(“Andy Griffith”).books
> 
> This assumes that your people database table has a column called “name”,  
> otherwise simple replace name by whatever your column is called (e.g  
> Person.find\_by\_fullname).  
> Or if you did split firstname/lastname in your database
> 
> Person.find\_by\_firstname\_and\_lastname(‘Andy’, ‘Griffith’).books
> 
> Matt

Thanks for both responses. I put in a simpler example than I needed to  
make it easier to explain. This is very good, but what if there are  
several foreign keys for the books table. e.g. I want to do a search to  
find all the books that where person from the people table = Andy  
Griffith, publisher from the publishers table = Westinghouse, printer  
from the printers table = Bob’s printing etc.

In other words, several conditions based of foreign keys at the same  
time. Exactly like conditions works for the find method except with  
foriegn keys. Am i reaching here?

The only other thing I can think of is to look up each foriegn key  
individually and then put the id’s into the conidtion of books.find but  
that seems to be working around the ORM. Of course, using like a search  
engine is abusing it to begin with…

Is that more clear? Thanks again.
