# RoR-Sql query

**URL:** <https://www.ruby-forum.com/t/ror-sql-query/175110>\
**Category:** Rails\
**Created:** [September 8, 2009, 7:25am UTC](https://www.ruby-forum.com/t/ror-sql-query/175110 "2009-09-08T07:25:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [September 8, 2009, 7:25am UTC](https://www.ruby-forum.com/t/ror-sql-query/175110/1 "2009-09-08T07:25:23Z")

</div>

Hi everyone

Consider there are three tables book,money,card in database.

Book has got column names name,price.

Money has got column names price,type.

Card has got column names type,id.

Now based on the ‘id’ of the ‘card’ i’ve to fetch ‘name’ of the ‘book’.

So i m writing a query in RoR model as

```
      Book.find(:all, :select => "book.name",
             :joins => "INNER JOIN money ON money.price =

```

book.price"+  
“INNER JOIN card ON card.type = money.type”,  
:conditions =\> “`card`.`id` = ‘10’”)

But this doesnt work.Is there any alternate query or i m missing  
something in this query.

Any help would be appreciated.

Thanks in advance

Anandh

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [September 8, 2009, 9:06am UTC](https://www.ruby-forum.com/t/ror-sql-query/175110/2 "2009-09-08T09:06:40Z")

</div>

Well, I don’t know the answer, but you can probably use the sql query  
mentioned in this page:  
[http://www.keithjbrown.co.uk/vworks/mysql/mysql\_p5.php](http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php) ( Do a browser  
search  
on string “Joining Three Tables”)

BTW, this email is to mention a different problem in your query: should  
not  
card be cards and book be books in your query (table names are, by  
convention, plural in rails) Try that too, might work.

Thanks,  
Abhinav

–  
à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

On Tue, Sep 8, 2009 at 10:55 AM, Anandh K. \<

---

<div class="post-metadata">

**Author:** ![anandhkumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@anandhkumar](https://www.ruby-forum.com/u/anandhkumar)\
**Post date:** [September 8, 2009, 10:35am UTC](https://www.ruby-forum.com/t/ror-sql-query/175110/3 "2009-09-08T10:35:03Z")

</div>

2009/9/8 Anandh K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> book.price"+  
> Â Â Â Â Â Â Â Â “INNER JOIN card ON card.type = money.type”,  
> Â Â Â Â Â Â Â Â :conditions =\> “`card`.`id` = ‘10’”)
> 
> But this doesnt work.Is there any alternate query or i m missing  
> something in this query.

Don’t use a column called type, it is a reserved word in rails as it  
is used in Single Table Inheritance. There are workarounds but much  
better not to use it if possible.

You have not told us what are the relationships between the models.  
If you set these up appropriately rails will almost always generate  
the joins for you. If you have to specify joins explicitly then  
possibly you have not set your relationships up correctly.

Colin
