# Find with join problem

**URL:** <https://www.ruby-forum.com/t/find-with-join-problem/48738>\
**Category:** Rails\
**Created:** [November 23, 2005, 11:07pm UTC](https://www.ruby-forum.com/t/find-with-join-problem/48738 "2005-11-23T23:07:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vpuzzella](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e47c2d/32.png) [@vpuzzella](https://www.ruby-forum.com/u/vpuzzella)\
**Post date:** [November 23, 2005, 11:07pm UTC](https://www.ruby-forum.com/t/find-with-join-problem/48738/1 "2005-11-23T23:07:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![vpuzzella](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e47c2d/32.png) [@vpuzzella](https://www.ruby-forum.com/u/vpuzzella)\
**Post date:** [November 24, 2005, 7:24am UTC](https://www.ruby-forum.com/t/find-with-join-problem/48738/2 "2005-11-24T07:24:03Z")

</div>

I had this problem. It can be solved without using find\_by\_sql:

[http://www.ruby-forum.com/topic/2510](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

---

<div class="post-metadata">

**Author:** ![vpuzzella](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e47c2d/32.png) [@vpuzzella](https://www.ruby-forum.com/u/vpuzzella)\
**Post date:** [November 24, 2005, 2:32pm UTC](https://www.ruby-forum.com/t/find-with-join-problem/48738/3 "2005-11-24T14:32:28Z")

</div>

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.
