Wrong id for activerecord object when using :joins in mysql

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

Hi,

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

On 12/21/05, Kelly Dwight F. [email protected] 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

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.

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.