# Display specific field that has duplicate name to other table

**URL:** <https://www.ruby-forum.com/t/display-specific-field-that-has-duplicate-name-to-other-table/205623>\
**Category:** Rails\
**Created:** [April 11, 2011, 9:11am UTC](https://www.ruby-forum.com/t/display-specific-field-that-has-duplicate-name-to-other-table/205623 "2011-04-11T09:11:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [April 11, 2011, 9:11am UTC](https://www.ruby-forum.com/t/display-specific-field-that-has-duplicate-name-to-other-table/205623/1 "2011-04-11T09:11:06Z")

</div>

HI TO ALL

I want to display a specific field in  
my VIEW but they have the same FIELD name…For example, th id in the  
PAYMENTS table and the id in the SUGGESTS table have the same field  
name… I want to be able to display the correct PAYMENTS id and  
correct SUGGESTS id…  
My Database is big im having a hard time to trace…which id it belongs  
to.

In my ruby on rails action or method Controller:  
def print\_two\_table  
@p = Pay.find\_by\_sql("SELECT p.id, p.topic, s.id, s.income, s.price FROM  
pays as P LEFT OUTER JOIN suggests AS s ON s.b\_id = p.b\_id WHERE  
p.created\_at BETWEEN ‘2008-03-04’ AND ‘2008-07-06’ ")  
end

In my View action or method:

\<%= @p.each do |form| %\>  
\<%= form.income %\>  
  
\<%= form.id %\>   
  
\<%= form.income %\>  
  
\<% end %\>

It gives me the wrong id i want to display…I want to display id in  
payments.

---

<div class="post-metadata">

**Author:** ![eianror](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c0e974/32.png) [@eianror](https://www.ruby-forum.com/u/eianror)\
**Post date:** [April 11, 2011, 9:18am UTC](https://www.ruby-forum.com/t/display-specific-field-that-has-duplicate-name-to-other-table/205623/2 "2011-04-11T09:18:26Z")

</div>

On Apr 11, 8:11am, Ad Ber [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In my ruby on rails action or method Controller:  
> def print\_two\_table  
> @p = Pay.find\_by\_sql("SELECT p.id, p.topic, s.id, s.income, s.price FROM  
> pays as P LEFT OUTER JOIN suggests AS s ON s.b\_id = p.b\_id WHERE  
> p.created\_at BETWEEN ‘2008-03-04’ AND ‘2008-07-06’ ")  
> end  
> If you’re going to do it like this (rather than using associations)  
> you’re going to need to alias the column names (ie select s.id as  
> suggest\_id)

Fred

---

<div class="post-metadata">

**Author:** ![eianror](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c0e974/32.png) [@eianror](https://www.ruby-forum.com/u/eianror)\
**Post date:** [April 11, 2011, 10:54am UTC](https://www.ruby-forum.com/t/display-specific-field-that-has-duplicate-name-to-other-table/205623/3 "2011-04-11T10:54:55Z")

</div>

Frederick C. wrote in post #992073:

> On Apr 11, 8:11am, Ad Ber [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > In my ruby on rails action or method Controller:  
> > def print\_two\_table  
> > @p = Pay.find\_by\_sql("SELECT p.id, p.topic, s.id, s.income, s.price FROM  
> > pays as P LEFT OUTER JOIN suggests AS s ON s.b\_id = p.b\_id WHERE  
> > p.created\_at BETWEEN ‘2008-03-04’ AND ‘2008-07-06’ ")  
> > end  
> > If you’re going to do it like this (rather than using associations)  
> > you’re going to need to alias the column names (ie select s.id as  
> > suggest\_id)
> 
> Fred

Uhm..I have already made the necessary relationships..do you have any  
idea how i can be able to display the proper id want to display?

---

<div class="post-metadata">

**Author:** ![eianror](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c0e974/32.png) [@eianror](https://www.ruby-forum.com/u/eianror)\
**Post date:** [April 11, 2011, 11:15am UTC](https://www.ruby-forum.com/t/display-specific-field-that-has-duplicate-name-to-other-table/205623/4 "2011-04-11T11:15:46Z")

</div>

On Apr 11, 9:54am, Ad Ber [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > you’re going to need to alias the column names (ie select s.id as  
> > suggest\_id)
> 
> > Fred
> 
> Uhm..I have already made the necessary relationships..do you have any  
> idea how i can be able to display the proper id want to display?

Well you’re not using them. if you were You’d do something like  
Pay.where( …).includes(:suggests)  
and then iterate over your pay objects and their associated suggest  
objects.  
If you don’t want to do that then like I said you need to alias the  
ambiguous column names so that they aren’t ambiguous anymore.

Fred
