Forum: Ruby on Rails wrong id for activerecord object when using :joins in mysql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Dwight Felkins (Guest)
on 2005-12-20 23:12
(Received via mailing list)
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
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 Peter Donald (Guest)
on 2005-12-20 23:30
(Received via mailing list)
Hi,

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

On 12/21/05, Kelly Dwight Felkins <railsinator@gmail.com> wrote:
> columns, the id of the main table, and the id of the join table. The objects
>  Thanks,
>
--
Cheers,

Peter Donald

Blog: http://www.RealityForge.org
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Dwight Felkins (Guest)
on 2005-12-20 23:36
(Received via mailing list)
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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2005-12-21 00:07
(Received via mailing list)
Kelly Dwight Felkins 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.
This topic is locked and can not be replied to.