# Printing field attributes outside the model.find\_by\_sql

**URL:** <https://discuss.rubyonrails.org/t/printing-field-attributes-outside-the-model-find-by-sql/58374>\
**Category:** rubyonrails-talk\
**Created:** [April 11, 2011, 2:42am UTC](https://discuss.rubyonrails.org/t/printing-field-attributes-outside-the-model-find-by-sql/58374 "2011-04-11T02:42:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 11, 2011, 2:42am UTC](https://discuss.rubyonrails.org/t/printing-field-attributes-outside-the-model-find-by-sql/58374/1 "2011-04-11T02:42:41Z")

</div>

Hi All,

I have Model called pays..and it has fields like(id, topic, subject, created\_at, b\_id) and i also have a Model called suggests with fields(id, income, price, b\_id)

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.message %\>\<br /\> \<%= form.income %\>\<br /\> (error on this part)

\<% end %\>

It gives me an error missing attribute

What i want to accomplish is to be able to print field attributes in the suggests model or table. Any help is appreciated..TY.

---

<div class="post-metadata">

**Author:** ![Todd\_A\_Jacobs2](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Todd\_A\_Jacobs2](https://discuss.rubyonrails.org/u/Todd_A_Jacobs2)\
**Post date:** [April 11, 2011, 2:55am UTC](https://discuss.rubyonrails.org/t/printing-field-attributes-outside-the-model-find-by-sql/58374/2 "2011-04-11T02:55:39Z")

</div>

> @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' ")

[snip]

> \<%= form.message %\>\<br /\> It gives me an error missing attribute

Perhaps because you aren't selecting a message attribute in your SQL statement?

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 11, 2011, 3:23am UTC](https://discuss.rubyonrails.org/t/printing-field-attributes-outside-the-model-find-by-sql/58374/3 "2011-04-11T03:23:01Z")

</div>

Todd A. Jacobs wrote in post #992052:

> > @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' ")
> 
> [snip]
> 
> > \<%= form.message %\>\<br /\> It gives me an error missing attribute
> 
> Perhaps because you aren't selecting a message attribute in your SQL statement?

Uhm..sorry that is an typo error..

what i means was

In my View action or method:

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

\<%= form.topic%\>\<br /\> \<%= form.income %\>\<br /\> (error on this part)

\<% end %\>

Im having an error on the form.income part..

---

<div class="post-metadata">

**Author:** ![Bryan\_Crossland](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@Bryan\_Crossland](https://discuss.rubyonrails.org/u/Bryan_Crossland)\
**Post date:** [April 11, 2011, 4:26am UTC](https://discuss.rubyonrails.org/t/printing-field-attributes-outside-the-model-find-by-sql/58374/4 "2011-04-11T04:26:06Z")

</div>

If you have relationships setup between your models then getting to the income should be a easy as form.suggest.income or form.suggests.first.income depending on the relationship type.

B.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [April 11, 2011, 5:41am UTC](https://discuss.rubyonrails.org/t/printing-field-attributes-outside-the-model-find-by-sql/58374/5 "2011-04-11T05:41:01Z")

</div>

Uhm..TY..I have configured the model relationships...I was ABLE to display form.income as long as i include the FIELD IN THE QUERY...

My follow up question is what if 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 the PAYMENTS id and correct SUGGESTS id.. My Database is big im having a hard time to trace..which id it belongs to. Please help me.
