Forum: Ruby on Rails find with join problem

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.
648fe430195d2ef4316a88cb6428d700?d=identicon&s=25 Vince P. (vpuzzella)
on 2005-11-23 23:07
Hi Folks,

Ruby Version: 1.8.2
Rails Version: 0.14.3

I am having a problem with the ActiveRecord::Base 'find' method.  The
problem occurs when I specify a join statement on a table that happens
to have fields with the same name.  In this case, both the students and
academic_supervisor tables have id, fisrtName and lastName fields.
Notice the differences between these to calls.  The academic_supervisor
values seem to be overwriting the student values.

Call #1:

irb(main):040:0* puts Student.find( :first ).to_yaml
--- !ruby/object:Student
attributes:
  lastName: Smith
  academic_supervisor_id: "13"
  firstName: Bob
  id: "8"
=> nil

Call #2 (with join)

irb(main):049:0* puts Student.find( :first, :joins => 'JOIN
academic_supervisors on academic_supervisors.id =
students.academic_supervisor_id' ).to_yaml
--- !ruby/object:Student
attributes:
  lastName: ''
  academic_supervisor_id: "13"
  firstName: ''
  id: "13"
=> nil

Am I missing something here? Is this a known issue?  Is there a
work-around?  BTW, I am using the 'paginate' method in my app, so
find_by_sql is not an option.

TIA!
498eab81f4aa4bdb77f32d75464e16e5?d=identicon&s=25 Christer N. (christer)
on 2005-11-24 07:24
I had this problem. It can be solved without using find_by_sql:

http://www.ruby-forum.com/topic/2510

You have to specify :select => "r.id" to get the correct table as there
are two table rows from the same table involved.

cheers
648fe430195d2ef4316a88cb6428d700?d=identicon&s=25 Vince P. (vpuzzella)
on 2005-11-24 14:32
Thanks Christer!

I added the :select option to the paginate call and it worked.  The RDoc
for paginate does not list :select as an option though.
This topic is locked and can not be replied to.