# Query Multiple tables

**URL:** <https://www.ruby-forum.com/t/query-multiple-tables/194899>\
**Category:** Rails\
**Created:** [September 13, 2010, 1:00pm UTC](https://www.ruby-forum.com/t/query-multiple-tables/194899 "2010-09-13T13:00:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 13, 2010, 1:00pm UTC](https://www.ruby-forum.com/t/query-multiple-tables/194899/1 "2010-09-13T13:00:10Z")

</div>

Hi,

select \* from table1 inner join table2 on table1.foreign\_key =  
table2.foreign\_key

select \* from table1 inner join table2 on table1.foreign\_key =  
table2.foreign\_key join table3 on table2.foreign\_key=table3.foreign\_key

These are the SQL commands, I need to apply it in Rails. Kindly tell me  
the  
syntax in Rails to apply these SQL commands instead of sql\_query.

–  
With Regards  
Palani Kannan. K.

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 13, 2010, 1:12pm UTC](https://www.ruby-forum.com/t/query-multiple-tables/194899/2 "2010-09-13T13:12:28Z")

</div>

On Sep 13, 11:59 am, PalaniKannan K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Take a look at the :joins option to find (or the joins scope  
modifiying thing in rails 3)

Fred

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 13, 2010, 7:55pm UTC](https://www.ruby-forum.com/t/query-multiple-tables/194899/3 "2010-09-13T19:55:16Z")

</div>

On Sep 13, 6:10 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Take a look at the :joins option to find (or the joins scope  
> modifiying thing in rails 3)
> 
> Fred

I think has\_many :through might also be what you’re looking for. You  
can avoid writing SQL 99% of the time if you learn how to use the  
various flavors of ActiveRecord associations.

Jeff

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 14, 2010, 9:53am UTC](https://www.ruby-forum.com/t/query-multiple-tables/194899/4 "2010-09-14T09:53:30Z")

</div>

On 13 September 2010 19:54, Jeff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> I think has\_many :through might also be what you’re looking for. You  
> can avoid writing SQL 99% of the time if you learn how to use the  
> various flavors of ActiveRecord associations.

Dear Jeff,  
In model I can apply has\_many and belongs\_to option. How I can apply  
those  
tables in Controller and View… I am new bee… I tried with some  
tutorials, every tutorial i m getting instructions to use has\_many and  
belongs\_to in model. I am getting problem with controllers and view to  
use  
“find” with foreign\_keys if I use those options in model.

> Jeff

–  
With Regards  
Palani Kannan. K
