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.
Kelly Dwight F. (Guest)
on 2005-12-21 00: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
Peter D. (Guest)
on 2005-12-21 00:30
(Received via mailing list)
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> 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
Kelly Dwight F. (Guest)
on 2005-12-21 00: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.
Mark Reginald J. (Guest)
on 2005-12-21 01:07
(Received via mailing list)
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.
This topic is locked and can not be replied to.