# Problems with getting correct id from query involving two tables

**URL:** <https://www.ruby-forum.com/t/problems-with-getting-correct-id-from-query-involving-two-tables/155230>\
**Category:** Rails\
**Created:** [December 9, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/problems-with-getting-correct-id-from-query-involving-two-tables/155230 "2008-12-09T17:37:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@ken](https://www.ruby-forum.com/u/ken)\
**Post date:** [December 9, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/problems-with-getting-correct-id-from-query-involving-two-tables/155230/1 "2008-12-09T17:37:25Z")

</div>

I have a query that is intended to find all “transfers” based on a  
condition the uses a second table. In the controller, it looks like  
this:

def find\_protocols  
@transfers = Transfer.find(:all, :from =\> “transfers,  
protocols”, :conditions =\> “transfers.protocol\_id = protocols.id AND  
protocols.name = “#{params[:protocol]}””)  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @transfers }  
end  
end

It works great, with one problem; the id’s associated with the  
rendered objects are “transfers.protocol\_id” and not  
“transfers.id” (or at any rate, they are certainly not  
“transfers.id”). Anyone have recommendations to fix this? I’d prefer  
something that allows me to stay at the SQL level, because I am quite  
comfortable working with the SQL queries.

Many thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![ken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@ken](https://www.ruby-forum.com/u/ken)\
**Post date:** [December 9, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/problems-with-getting-correct-id-from-query-involving-two-tables/155230/2 "2008-12-09T17:42:43Z")

</div>

you add…

:select =\> “transfers.\*”

---

<div class="post-metadata">

**Author:** ![ken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@ken](https://www.ruby-forum.com/u/ken)\
**Post date:** [December 9, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/problems-with-getting-correct-id-from-query-involving-two-tables/155230/3 "2008-12-09T18:27:59Z")

</div>

Thanks. I also found that reversing the order of the tables worked,  
but the select will be a lot more reliable.

Thanks,  
Ken

---

<div class="post-metadata">

**Author:** ![ken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@ken](https://www.ruby-forum.com/u/ken)\
**Post date:** [December 9, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/problems-with-getting-correct-id-from-query-involving-two-tables/155230/4 "2008-12-09T18:32:06Z")

</div>

You should NEVER do this:

@transfers = Transfer.find(:all, :from =\> “transfers, protocols”,  
:conditions =\> “transfers.protocol\_id = protocols.id AND  
protocols.name = "#{params[:protocol]}"”)

You’re opening up your site for SQL injection attacks, do it using  
placeholder variables:

@transfers = Transfer.find(:all, :from =\> “transfers, protocols”,  
:conditions =\> [“transfers.protocol\_id = protocols.id AND  
protocols.name = ?”, params[:protocol] ])

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)
