# Wrong id for activerecord object when using :joins in mysql

**URL:** <https://www.ruby-forum.com/t/wrong-id-for-activerecord-object-when-using-joins-in-mysql/50763>\
**Category:** Rails\
**Created:** [December 20, 2005, 11:12pm UTC](https://www.ruby-forum.com/t/wrong-id-for-activerecord-object-when-using-joins-in-mysql/50763 "2005-12-20T23:12:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aseldo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@aseldo](https://www.ruby-forum.com/u/aseldo)\
**Post date:** [December 20, 2005, 11:12pm UTC](https://www.ruby-forum.com/t/wrong-id-for-activerecord-object-when-using-joins-in-mysql/50763/1 "2005-12-20T23:12:42Z")

</div>

I’m using a find with a :joins clause, like this:

:joins =\> “JOIN table\_b ON table\_b.id=table\_b\_id”

The records returned are used in a scaffold generated list page. The  
edit,  
show, and destroy links are getting the wrong id. They are getting the  
id of  
the table b record.

When I run the query using mysql, I see that the query returns _2_ id  
columns, the id of the main table, and the id of the join table. The  
objects  
returned have only one id, and apparently its the last one. It would be  
better for it to be the first one.

Any suggestions for me?

This is used with paginate.

I think I’m using the native ruby mysql adapter, as compared to the c  
version.

Thanks,

-Kelly

---

<div class="post-metadata">

**Author:** ![aseldo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@aseldo](https://www.ruby-forum.com/u/aseldo)\
**Post date:** [December 20, 2005, 11:30pm UTC](https://www.ruby-forum.com/t/wrong-id-for-activerecord-object-when-using-joins-in-mysql/50763/2 "2005-12-20T23:30:54Z")

</div>

Hi,

Remove the id column from your join column and it should work.

On 12/21/05, Kelly Dwight F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> columns, the id of the main table, and the id of the join table. The objects  
> Thanks,

–  
Cheers,

Peter D.

Blog: [http://www.RealityForge.org](http://www.RealityForge.org)

---

<div class="post-metadata">

**Author:** ![aseldo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@aseldo](https://www.ruby-forum.com/u/aseldo)\
**Post date:** [December 20, 2005, 11:36pm UTC](https://www.ruby-forum.com/t/wrong-id-for-activerecord-object-when-using-joins-in-mysql/50763/3 "2005-12-20T23:36:58Z")

</div>

How would I do that? Adding :joins brings in all the columns from the  
join  
table. Also, the id is the column we are joining on.

---

<div class="post-metadata">

**Author:** ![aseldo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@aseldo](https://www.ruby-forum.com/u/aseldo)\
**Post date:** [December 21, 2005, 12:07am UTC](https://www.ruby-forum.com/t/wrong-id-for-activerecord-object-when-using-joins-in-mysql/50763/4 "2005-12-21T00:07:14Z")

</div>

Kelly Dwight F. wrote:

> I’m using a find with a :joins clause, like this:
> 
> :joins =\> “JOIN table\_b ON table\_b.id=table\_b\_id”
> 
> When I run the query using mysql, I see that the query returns _2_ id  
> columns, the id of the main table, and the id of the join table. The  
> objects returned have only one id, and apparently its the last one. It  
> would be better for it to be the first one.

Either use :select =\> ‘table\_a.\*, table\_b.some\_data, …’,  
or instead of the join use a belongs\_to association plus  
find’s :include option.

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