# Problem with joins and overlapping field names

**URL:** <https://www.ruby-forum.com/t/problem-with-joins-and-overlapping-field-names/61046>\
**Category:** Rails\
**Created:** [May 19, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/problem-with-joins-and-overlapping-field-names/61046 "2006-05-19T16:56:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [May 19, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/problem-with-joins-and-overlapping-field-names/61046/1 "2006-05-19T16:56:08Z")

</div>

Hi,

I have the following code:  
@conversation\_pages,  
@conversations = paginate(:conversations,  
:per\_page =\> 20,  
:joins =\> 'LEFT JOIN messages on  
messages.conversation\_id = conversations.id '+  
‘LEFT JOIN email\_addresses  
on  
email\_addresses.id = messages.email\_address\_id’,  
:conditions =\>  
[‘email\_addresses.email =  
?’, @email\_address.email])

producing this sql staement:

SELECT \* FROM conversations LEFT JOIN messages on  
messages.conversation\_id =  
conversations.id LEFT JOIN email\_addresses on email\_addresses.id =  
messages.email\_address\_id WHERE (email\_addresses.email =  
‘removed\_email\_address@domain.invalid’)  
LIMIT 0, 20

The funny thing is that my conversation.id is no longer referencing  
the  
conversation.id but email\_addresses.id. I assume this is due to the fact  
that “\*” is generated in the sql, isn’t it? I haven’t found a way to  
instruct active record to use explicit column names like  
conversations.id,  
email\_addressesses.id etc. etc.

Any idea how to work around that problem?

Cheers,  
Mariano

---

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [May 20, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/problem-with-joins-and-overlapping-field-names/61046/2 "2006-05-20T13:39:57Z")

</div>

Mariano K. wrote:

> The funny thing is that my conversation.id [http://conversation.id](http://conversation.id) is  
> no longer referencing the conversation.id [http://conversation.id](http://conversation.id) but  
> email\_addresses.id. I assume this is due to the fact that “\*” is  
> generated in the sql, isn’t it? I haven’t found a way to instruct active  
> record to use explicit column names like conversations.id  
> [http://conversations.id](http://conversations.id), email\_addressesses.id etc. etc.
> 
> Any idea how to work around that problem?

Try either replacing the joins option with  
:include =\> {:messages =\> :email\_address}

or use a :select option to select only the fields you want. e.g.  
:select =\> ‘conversations.\*, messages.content, email\_addresses.email’

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Mariano\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eada6e/32.png) [@Mariano\_K](https://www.ruby-forum.com/u/Mariano_K)\
**Post date:** [August 22, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/problem-with-joins-and-overlapping-field-names/61046/3 "2006-08-22T14:16:55Z")

</div>

Mark Reginald J. wrote:

> Mariano K. wrote:
> 
> > The funny thing is that my conversation.id [http://conversation.id](http://conversation.id) is  
> > no longer referencing the conversation.id [http://conversation.id](http://conversation.id) but  
> > email\_addresses.id. \>
> 
> Try either replacing the joins option with  
> :include =\> {:messages =\> :email\_address}
> 
> or use a :select option to select only the fields you want. e.g.  
> :select =\> ‘conversations.\*, messages.content, email\_addresses.email’

I had the same problem. My tables are associated like this:

stocks = each item of stock (belongs\_to product)  
products = all the products we sell (belongs\_to product\_type and  
has\_many stocks)  
product\_types = a grouping of products (has\_many products)

I was using:

```
@stocks = Stock.find(:all,
                      :conditions => "pr.product_type_id = 

```

#{@product\_type.id}",  
:joins =\> “as st right join products as pr on  
st.product\_id = pr.id”)

The result was that when I tried to use stock.id in the resulting view,  
the system was returning the product.id. Having just found this posting,  
I updated the @stocks declaration to:

```
@stocks = Stock.find(:all,
                      :select => "st.*",
                      :conditions => "pr.product_type_id = 

```

#{@product\_type.id}",  
:joins =\> “as st right join products as pr on  
st.product\_id = pr.id”)

The :select =\> “st.\*” entry did the trick. stock.id is now returning the  
correct id.

Thank you Mark.
