# Printing nested data to screen

**URL:** <https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932>\
**Category:** rubyonrails-talk\
**Created:** [July 19, 2011, 1:23am UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932 "2011-07-19T01:23:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barney1](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@Barney1](https://discuss.rubyonrails.org/u/Barney1)\
**Post date:** [July 19, 2011, 1:23am UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/1 "2011-07-19T01:23:59Z")

</div>

I have a table called people\_skills linked to a people table by a FK named people\_id. Trying to get the people\_skills fields printed on a "Show" screen has never given me what I wanted (the field value) without resorting to a helper which forms an input box. The people table's values are printed but the results of the people\_skills are either non-existent or strange (e.g. #\<ActiveRecord::Relation: 0x1d12778\>). This is the controller code (people\_controller.rb):

def show &nbsp;&nbsp;&nbsp;&nbsp;@person = Person.find(params[:id])

&nbsp;&nbsp;&nbsp;&nbsp;@people\_skill = PeopleSkill.select('skill').where(":people\_id = @person.id") &nbsp;&nbsp;&nbsp;&nbsp;show\_skill = @people\_skill.build.skill &nbsp;&nbsp;&nbsp;&nbsp;yrs\_exp = @people\_skill.build.years\_of\_experience

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @person, :xml =\> @peopleskill } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

and this is the show.html.erb:

\<p\> &nbsp;&nbsp;\<b\>Skill\</b\> &nbsp;&nbsp;\<%= @people\_skill.build.skill %\> &nbsp;&nbsp;\<%= PeopleSkill.where("people\_id = @person.id")%\> \</p\>

which produced the strange #\<ActiveRecord... output above. Other guesses have either given me errors or no output at all, so please tell me the right way to do this. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

---

<div class="post-metadata">

**Author:** ![Andrew\_Skegg](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Andrew\_Skegg](https://discuss.rubyonrails.org/u/Andrew_Skegg)\
**Post date:** [July 19, 2011, 2:14am UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/2 "2011-07-19T02:14:08Z")

</div>

Barney \<bsperlin@...\> writes:

> \<p\> &nbsp;&nbsp;\<b\>Skill\</b\> &nbsp;&nbsp;\<%= @people\_skill.build.skill %\> &nbsp;&nbsp;\<%= PeopleSkill.where("people\_id = @person.id")%\> \</p\>
> 
> which produced the strange #\<ActiveRecord... output above. Other guesses have either given me errors or no output at all, so please tell me the right way to do this. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

PeopleSkill.where("people\_id = @person.id") will return an ActiveRecord relation object, rather that execute the query againstg the database. Try: PeopleSkill.where("people\_id = @person.id").find

---

<div class="post-metadata">

**Author:** ![Barney1](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@Barney1](https://discuss.rubyonrails.org/u/Barney1)\
**Post date:** [July 19, 2011, 3:26pm UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/3 "2011-07-19T15:26:34Z")

</div>

Thanks Andrew, but I couldn't make the 'find' work on the 'PeopleSkill.where...' line. When I used 'find' on the @people\_skill line it came very close (since in the controller, listed above, people\_skill was already choosing the field 'skill'), so when I used: \<%= @people\_skill.find(@person.id) and skipped the next line listed above, the SQLException was SQLite3::SQLException: near ".": syntax error: SELECT skill FROM "people\_skills" WHERE "people\_skills"."id" = 23 AND (:people\_id = @person.id) LIMIT 1 which is VERY close to what I want, except that I want "people\_skills"."people\_id"=23, not "people\_skill"."id"=23. I know that 'find' always looks for the 'id' so I've got to get a different variation. What changes should I make? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thanks again, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

---

<div class="post-metadata">

**Author:** ![Andrew\_Skegg](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Andrew\_Skegg](https://discuss.rubyonrails.org/u/Andrew_Skegg)\
**Post date:** [July 20, 2011, 12:17am UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/4 "2011-07-20T00:17:02Z")

</div>

Barney \<bsperlin@...\> writes:

> Thanks Andrew, but I couldn't make the 'find' work on the 'PeopleSkill.where...' line. When I used 'find' on the @people\_skill line it came very close (since in the controller, listed above, people\_skill was already choosing the field 'skill'), so when I used: \<%= @people\_skill.find(@person.id) and skipped the next line listed above, the SQLException was SQLite3::SQLException: near ".": syntax error: SELECT skill FROM "people\_skills" WHERE "people\_skills"."id" = 23 AND (:people\_id = @person.id) LIMIT 1 which is VERY close to what I want, except that I want "people\_skills"."people\_id"=23, not "people\_skill"."id"=23. I know that 'find' always looks for the 'id' so I've got to get a different variation. What changes should I make? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thanks again, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

What if we flip the logic?

Assuming your models look something like:

Person \< ActiveRecord::Base &nbsp;&nbsp;has\_and\_belongs\_to\_many :skills end

Skill \< ActiveRecord::Base &nbsp;&nbsp;has\_and\_belongs\_to\_many :people end

With the appropriate PeopleSkills join table in the database.

Then:

@person = Person.find(params[:id]) @people\_skill = @person.skills

Or more easily:

def show &nbsp;&nbsp;&nbsp;&nbsp;@person = Person.find(params[:id]) end

and the view becomes:

\<p\> &nbsp;&nbsp;\<b\>Skills\</b\> &nbsp;&nbsp;\<%= @people.skills \> \</p\>

or similar.

---

<div class="post-metadata">

**Author:** ![Eric\_Hu](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@Eric\_Hu](https://discuss.rubyonrails.org/u/Eric_Hu)\
**Post date:** [July 20, 2011, 2:30am UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/5 "2011-07-20T02:30:07Z")

</div>

The original code didn’t work because Barney was generating an ActiveRecord Relation. The .find almost worked, except he wanted to embed Ruby code in a SQL statement. This should do it:

PeopleSkill.where(“people\_id = #{@[person.id](http://person.id/)}”).find

In the future, I think something like Andrew’s suggestion would be a lot more readable and maintainable–I.E. using ActiveRecord to convert your database information into objects, and looking for your information among the objects instead of at the database level.

---

<div class="post-metadata">

**Author:** ![Barney1](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@Barney1](https://discuss.rubyonrails.org/u/Barney1)\
**Post date:** [July 21, 2011, 8:30pm UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/6 "2011-07-21T20:30:40Z")

</div>

Hi Andrew, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Currently "show" works from the listings page but "new" and "edit" don't. The current people\_controller is:

def show &nbsp;&nbsp;&nbsp;&nbsp;@person = Person.find(params[:id])

&nbsp;&nbsp;&nbsp;&nbsp;@people\_skills = PeopleSkill.all

&nbsp;&nbsp;&nbsp;&nbsp;@people\_skill = PeopleSkill.select('skill').where("{person\_id =\> @person.id}")

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @person, :xml =\> @peopleskill } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

and the people\_skills\_controller.rb#show is:

def show &nbsp;&nbsp;&nbsp;&nbsp;@people\_skill = PeopleSkill.find(params[:id])

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @people\_skill } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

The people\_controller.rb#edit, which doesn't work, is:

def edit &nbsp;&nbsp;&nbsp;&nbsp;@person = Person.find(params[:id])

&nbsp;&nbsp;&nbsp;&nbsp;@people\_skills = PeopleSkill.all

&nbsp;&nbsp;&nbsp;&nbsp;@people\_skill = PeopleSkill.select('skill').where("{person\_id =\> @person.id}")

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @person, :xml =\> @peopleskill } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

and the people\_skills\_controller.rb#edit is:

def edit &nbsp;&nbsp;&nbsp;&nbsp;@people\_skill = PeopleSkill.select("skill").where("{person\_id =\> @person.id}") #find(params[:person\_id])

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @people\_skill } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

and these give an error in the \people\_skills\_form.html.erb at the first line:

%= form\_for(@people\_skill) do |f| %\> &nbsp;&nbsp;\<% if @people\_skill.errors.any? %\>

which is: undefined method `model\_name' for ActiveRecord::Relation:Class and if I cut one @people\_skill out of a controller I get: undefined method `model\_name' for NilClass:Class

and I take these both to mean that @people\_skill doesn't exist. Could the 2 @people\_skill instances be colliding? Or the second shadowing the first?

Any further help would be appreciated and I know you've put more into this than you need to! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

---

<div class="post-metadata">

**Author:** ![Barney1](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@Barney1](https://discuss.rubyonrails.org/u/Barney1)\
**Post date:** [July 21, 2011, 8:33pm UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/7 "2011-07-21T20:33:30Z")

</div>

Hi Eric,

Well, I couldn't make the objects work:

@people\_skill = @person.skill

wasn't accepted. I'll keep trying and any suggestions are appreciated. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

---

<div class="post-metadata">

**Author:** ![Andrew\_Skegg](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Andrew\_Skegg](https://discuss.rubyonrails.org/u/Andrew_Skegg)\
**Post date:** [July 21, 2011, 11:31pm UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/8 "2011-07-21T23:31:18Z")

</div>

Barney \<bsperlin@...\> writes:

> The people\_controller.rb#edit, which doesn't work, is:
> 
> def edit &nbsp;&nbsp;&nbsp;&nbsp;@person = Person.find(params[:id])
> 
> &nbsp;&nbsp;&nbsp;&nbsp;@people\_skills = PeopleSkill.all
> 
> &nbsp;&nbsp;&nbsp;&nbsp;@people\_skill = PeopleSkill.select('skill').where("{person\_id =\> @person.id}")
> 
> &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @person, :xml =\> @peopleskill } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> and the people\_skills\_controller.rb#edit is:
> 
> def edit &nbsp;&nbsp;&nbsp;&nbsp;@people\_skill = PeopleSkill.select("skill").where("{person\_id =\> @person.id}") #find(params[:person\_id])
> 
> &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # show.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @people\_skill } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> and these give an error in the \people\_skills\_form.html.erb at the first line:
> 
> %= form\_for(@people\_skill) do |f| %\> &nbsp;&nbsp;\<% if @people\_skill.errors.any? %\>
> 
> which is: undefined method `model\_name' for ActiveRecord::Relation:Class and if I cut one @people\_skill out of a controller I get: undefined method `model\_name' for NilClass:Class
> 
> and I take these both to mean that @people\_skill doesn't exist. Could the 2 @people\_skill instances be colliding? Or the second shadowing the first?
> 
> Any further help would be appreciated and I know you've put more into this than you need to! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

Again, the controller is returning a \*relation\*, rather than the actual results. Try changing:

@people\_skill = PeopleSkill.select('skill').where("{person\_id =\> @person.id}")

to

@people\_skill = PeopleSkill.select('skill').where("{person\_id =\> @person.id}").all

Essentially this tells rails you are finished building the query and are prepared for results - it will trigger a query against database.

I still can't help feeling you are experiencing these issues because you are starting to swim upstream against the rails conventions. When things seem difficult, there is usually some ruby voodoo missing. Knowing how to work ruby and rails magic involves learning all the spells.

If you are still having difficulty, please post you model logic as well - especially the relationships between the objects (belongs\_to, has\_many, etc)

---

<div class="post-metadata">

**Author:** ![Curtis\_Schofield](https://avatars.discourse-cdn.com/v4/letter/c/96bed5/32.png) [@Curtis\_Schofield](https://discuss.rubyonrails.org/u/Curtis_Schofield)\
**Post date:** [July 21, 2011, 11:48pm UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/9 "2011-07-21T23:48:20Z")

</div>

> I still can't help feeling you are experiencing these issues because you are starting to swim upstream against the rails conventions.

One of the unspoken conventions being violated is putting Queries in the Controller - ☹

> If you are still having difficulty, please post you model logic as well - especially the relationships between the objects (belongs\_to, has\_many, etc)

This is a really good suggestion. Spending time expressing the logic and what your purpose is will increase the quality of help that you get - and likely reduce frustration.

---

<div class="post-metadata">

**Author:** ![Barney1](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@Barney1](https://discuss.rubyonrails.org/u/Barney1)\
**Post date:** [July 25, 2011, 10:48pm UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/10 "2011-07-25T22:48:41Z")

</div>

Hi Andrew and Curtis, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I've been away for 3 days but have come back to the problem above. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;As things have continued to be difficult I have backed off keeping things well normalized and have crunched the people\_skills table, with its 3 fields of: skill, competency and years of experience, into the people table. This still leaves another table, employee\_infos, in a one-one relationship with people which is still not printing on the same screen as people. Here are the models in question:

class Person \< ActiveRecord::Base &nbsp;&nbsp;default\_scope :order =\> 'last\_name' &nbsp;&nbsp;has\_one :EmployeeInfo &nbsp;&nbsp;has\_one :CandidateInfo &nbsp;&nbsp;has\_one :EmploymentHistory

&nbsp;&nbsp;validates :first\_name, :presence =\> true &nbsp;&nbsp;validates :last\_name, :presence =\> true &nbsp;&nbsp;validates :email, :presence =\> true end

class EmployeeInfo \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :person end

The people\_controller.rb#index, where I want to see the proper data, is:

def index &nbsp;&nbsp;&nbsp;&nbsp;@people = Person.all &nbsp;&nbsp;&nbsp;&nbsp;@employee\_infos = EmployeeInfo.all

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { render :xml =\> @people } &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

And a relevant section of index.html.erb is: \<%= @people.each do |person| %\> &nbsp;&nbsp;\<tr\> ... &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= person.zip\_code %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= person.skill\_set %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= WHAT GOES HERE TO BE ABLE TO PRINT THE "position" FIELD OF THE employee\_infos TABLE? WHAT CHANGES SHOULD BE MADE TO THE ABOVE?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A second point was made about not having SQL statements in the controller (which I have done) so what is the alternative? What pages can I go to to see some examples of the right way? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thanks again, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Barney

---

<div class="post-metadata">

**Author:** ![Andrew\_Skegg](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Andrew\_Skegg](https://discuss.rubyonrails.org/u/Andrew_Skegg)\
**Post date:** [July 29, 2011, 12:03am UTC](https://discuss.rubyonrails.org/t/printing-nested-data-to-screen/59932/11 "2011-07-29T00:03:55Z")

</div>

Barney \<bsperlin@...\> writes:

> And a relevant section of index.html.erb is: \<%= @people.each do |person| %\> &nbsp;&nbsp;\<tr\> ... &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= person.zip\_code %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= person.skill\_set %\>\</td\> &nbsp;&nbsp;&nbsp;&nbsp;\<td\>\<%= WHAT GOES HERE TO BE ABLE TO PRINT THE "position" FIELD OF THE employee\_infos TABLE? WHAT CHANGES SHOULD BE MADE TO THE ABOVE?

If I am reading this right:

\<td\>\<%= person.employee\_info.position %\>\</td\>

Note: this will trigger a database query to match the record in employee\_info with the person's ID. Your @employee\_infos variable is not being touched.  
You really want eager load:

@people = Person.all.include(:employee\_info)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A second point was made about not having SQL statements in the controller (which I have done) so what is the alternative? What pages can I go to to see some examples of the right way?

The seminal blog post was this [http://weblog.jamisbuck.org/2006/10/18/skinny-](http://weblog.jamisbuck.org/2006/10/18/skinny-) controller-fat-model
