# Problem with Joins :(

**URL:** <https://www.ruby-forum.com/t/problem-with-joins/69854>\
**Category:** Rails\
**Created:** [September 25, 2006, 1:22am UTC](https://www.ruby-forum.com/t/problem-with-joins/69854 "2006-09-25T01:22:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blewis](https://www.ruby-forum.com/u/blewis)\
**Post date:** [September 25, 2006, 1:22am UTC](https://www.ruby-forum.com/t/problem-with-joins/69854/1 "2006-09-25T01:22:52Z")

</div>

@document\_pages, @documents = paginate :documents, :per\_page =\> 20,  
:joins =\> “LEFT JOIN companies ON companies.id = documents.company\_id”,  
:conditions =\> “doc\_type\_id = ‘#{@doc\_type\_id}’”, :order =\>  
“companies.name asc”

The problem with this segment of code is that when I try to display a  
document’s ID number, it displays the companies’ ID number, since the  
join overwrites @documents.id with companies.id.

How can I prevent this?

Thanks,  
Ben L.

---

<div class="post-metadata">

**Author:** ![blewis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@blewis](https://www.ruby-forum.com/u/blewis)\
**Post date:** [September 25, 2006, 10:46am UTC](https://www.ruby-forum.com/t/problem-with-joins/69854/2 "2006-09-25T10:46:34Z")

</div>

Ben L. wrote:

> @document\_pages, @documents = paginate :documents, :per\_page =\> 20,  
> :joins =\> “LEFT JOIN companies ON companies.id = documents.company\_id”,  
> :conditions =\> “doc\_type\_id = ‘#{@doc\_type\_id}’”, :order =\>  
> “companies.name asc”
> 
> The problem with this segment of code is that when I try to display a  
> document’s ID number, it displays the companies’ ID number, since the  
> join overwrites @documents.id with companies.id.
> 
> How can I prevent this?
> 
> Thanks,  
> Ben L.

If you are doing the join just so that you can sort on companies.name,  
then I suppose the easiest thing is to use :select =\> ‘documents.\*’

Fred
