# Rails theory question, where to put a join query

**URL:** <https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180>\
**Category:** Rails\
**Created:** [August 16, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180 "2006-08-16T17:56:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gene\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gene\_h](https://www.ruby-forum.com/u/gene_h)\
**Post date:** [August 16, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180/1 "2006-08-16T17:56:56Z")

</div>

The is something I wrestle with a bit when I construct joined queries…  
Perhaps some experienced insight will straighten this out for me.

Say you have two tables with two models, ‘authors’ and ‘posts’.

an author has\_many posts, a post has\_one author.

You need methods to do the following queries:

find\_author(postid)  
find\_posts(authorid)

And you are doing a custom join query for both. How do you decide what  
model they go into? To me either can make sense, so I am uncomfortable  
with  
picking one.

Could I get some comments? Thanks!

---

<div class="post-metadata">

**Author:** ![gene\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gene\_h](https://www.ruby-forum.com/u/gene_h)\
**Post date:** [August 16, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180/2 "2006-08-16T18:06:13Z")

</div>

On 8/16/06, Gene H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> find\_author(postid)  
> find\_posts(authorid)
> 
> And you are doing a custom join query for both. How do you decide what  
> model they go into? To me either can make sense, so I am uncomfortable with  
> picking one.

What sort of custom join queries?

It seems that post.author and author.posts, the builtin association  
methods,  
are just the ticket.

jeremy

---

<div class="post-metadata">

**Author:** ![gene\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gene\_h](https://www.ruby-forum.com/u/gene_h)\
**Post date:** [August 16, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180/3 "2006-08-16T18:27:29Z")

</div>

Hmm… You make sense. When I first started I didn’t really understand  
things and so I was doing something like:

find\_by\_sql(“SELECT \* FROM authors,posts WHERE authors.id =  
posts.authorid”)

But you’re right I can just do an association can’t I?

Is there no extra overhead to doing the association? I’ve read about  
inefficiencies with Rails queries (ie. generating many unnecessary  
queries)… Maybe that was in a many to many situation. Maybe I’m being  
a  
bit paranoid.

An association would certainly be easier!

---

<div class="post-metadata">

**Author:** ![gene\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gene\_h](https://www.ruby-forum.com/u/gene_h)\
**Post date:** [August 16, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180/4 "2006-08-16T18:36:30Z")

</div>

2006/8/16, Gene H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> queries)… Maybe that was in a many to many situation. Maybe I’m being a  
> bit paranoid.

maybe there is an extra overhead, maybe not… if you are a SQL freak  
(like  
me 😛 ) the way to know it is watching the log, if both methods work for  
you. If you are in Linux/UNIX flavored, you can tail -f  
log/development.log  
(or production.log or server.log, as your project runs).

Probably MySQL will be more/less (but never equally) optimized than  
PostgreSQL, according to the engine’s way to get the data.

Very funny, it shows the log in colours!

---

<div class="post-metadata">

**Author:** ![gene\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gene\_h](https://www.ruby-forum.com/u/gene_h)\
**Post date:** [August 16, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180/5 "2006-08-16T18:36:30Z")

</div>

> Say you have two tables with two models, ‘authors’ and ‘posts’.

> an author has\_many posts, a post has\_one author.

A post belongs\_to author would be the right way.

> You need methods to do the following queries:
> 
> find\_author(postid)  
> find\_posts(authorid)
> 
> And you are doing a custom join query for both. How do you decide what  
> model they go into? To me either can make sense, so I am uncomfortable with  
> picking one.

Why do you do custom joins? Use associations.

## Saludos! HoraPe

Horacio J.  
Peñremoved\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gene\_h](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gene\_h](https://www.ruby-forum.com/u/gene_h)\
**Post date:** [August 16, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/rails-theory-question-where-to-put-a-join-query/65180/6 "2006-08-16T18:42:31Z")

</div>

> Hmm… You make sense. When I first started I didn’t really understand  
> things and so I was doing something like:

> find\_by\_sql(“SELECT \* FROM authors,posts WHERE authors.id = posts.authorid”)

> But you’re right I can just do an association can’t I?

> Is there no extra overhead to doing the association? I’ve read about  
> inefficiencies with Rails queries (ie. generating many unnecessary  
> queries)… Maybe that was in a many to many situation. Maybe I’m being a  
> bit paranoid.

If you want to avoid the extra queries you can use :include option so it  
gets all the data on just one query.

## Saludos! HoraPe

Horacio J.  
Peñremoved\_email\_address@domain.invalid  
removed\_email\_address@domain.invalid
