# To query multiple tables

**URL:** <https://www.ruby-forum.com/t/to-query-multiple-tables/126322>\
**Category:** Rails\
**Created:** [January 17, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/to-query-multiple-tables/126322 "2008-01-17T13:00:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bpankaj](https://www.ruby-forum.com/u/bpankaj)\
**Post date:** [January 17, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/to-query-multiple-tables/126322/1 "2008-01-17T13:00:25Z")

</div>

Hi every body,

I have a database containing around 15 tables. There are lots of  
relationships. Now if I need to add a query involving say 8  
tables(a,b,c,d,e,f,g,h) can I do it using find\_by\_sql. If so where do I  
implement the method, inside the class of which table.

Thanks

---

<div class="post-metadata">

**Author:** ![bpankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bpankaj](https://www.ruby-forum.com/u/bpankaj)\
**Post date:** [January 17, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/to-query-multiple-tables/126322/2 "2008-01-17T13:27:48Z")

</div>

On 17 Jan 2008, at 12:00, Pankaj jyoti Barua wrote:

> Hi every body,
> 
> I have a database containing around 15 tables. There are lots of  
> relationships. Now if I need to add a query involving say 8  
> tables(a,b,c,d,e,f,g,h) can I do it using find\_by\_sql.  
> You can do anything you want pretty much using find\_by\_sql  
> If so where do I  
> implement the method, inside the class of which table.

If you call Foo.find\_by\_sql will return an array of objects of class  
Foo. With that in mind it’s completely up to you

Fred

---

<div class="post-metadata">

**Author:** ![bpankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bpankaj](https://www.ruby-forum.com/u/bpankaj)\
**Post date:** [January 17, 2008, 1:49pm UTC](https://www.ruby-forum.com/t/to-query-multiple-tables/126322/3 "2008-01-17T13:49:21Z")

</div>

Frederick C. wrote:

> On 17 Jan 2008, at 12:00, Pankaj jyoti Barua wrote:
> 
> > Hi every body,
> > 
> > I have a database containing around 15 tables. There are lots of  
> > relationships. Now if I need to add a query involving say 8  
> > tables(a,b,c,d,e,f,g,h) can I do it using find\_by\_sql.  
> > You can do anything you want pretty much using find\_by\_sql  
> > If so where do I  
> > implement the method, inside the class of which table.
> 
> If you call Foo.find\_by\_sql will return an array of objects of class  
> Foo. With that in mind it’s completely up to you
> 
> Fred

Thanks.

So I will have to create a class called foo. From where should I call  
foo. Actually I have created the database using migration and I have  
implemented a cgi script that gives a front end to query the database.  
I am finding problem with some queries now which are preety complex.

---

<div class="post-metadata">

**Author:** ![bpankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bpankaj](https://www.ruby-forum.com/u/bpankaj)\
**Post date:** [January 17, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/to-query-multiple-tables/126322/4 "2008-01-17T14:26:36Z")

</div>

It would be better to use Model.connection.execute, this will take an  
sql command, and the name that goes into the log file.

WW

On Jan 17, 7:49 am, Pankaj jyoti Barua \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![bpankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bpankaj](https://www.ruby-forum.com/u/bpankaj)\
**Post date:** [January 17, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/to-query-multiple-tables/126322/5 "2008-01-17T18:59:11Z")

</div>

On Jan 17, 4:00 am, Pankaj jyoti Barua \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> I have a database containing around 15 tables. There are lots of  
> relationships. Now if I need to add a query involving say 8  
> tables(a,b,c,d,e,f,g,h) can I do it using find\_by\_sql. If so where do I  
> implement the method, inside the class of which table.

Just pick one. 🙂 It doesn’t much matter to you what kind of object is  
returned by find\_by\_sql (or connection.execute). It’ll have attributes  
for every column in the SELECT. So pick one as a “controller” (our  
term, not Rails’) and go through it. It’s really just a matter of self-  
documentation. In what model would you look for this method?

///ark
