Find with join problem


#1

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!


#2

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


#3

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.