Printing nested data to screen

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
@person = Person.find(params[:id])

@people_skill = PeopleSkill.select('skill').where(":people_id =

@person.id")
show_skill = @people_skill.build.skill
yrs_exp = @people_skill.build.years_of_experience

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @person, :xml => @peopleskill }
end

end

and this is the show.html.erb:

Skill <%= @people_skill.build.skill %> <%= PeopleSkill.where("people_id = @person.id")%>

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.
Barney

Barney <bsperlin@…> writes:

         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

Barney <bsperlin@…> writes:

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?
Thanks again,
Barney

What if we flip the logic?

Assuming your models look something like:

Person < ActiveRecord::Base
has_and_belongs_to_many :skills
end

Skill < ActiveRecord::Base
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
@person = Person.find(params[:id])
end

and the view becomes:

Skills <%= @people.skills >

or similar.

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}”).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.

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?
Thanks again,
Barney

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.
Barney

Hi Andrew,
Currently “show” works from the listings page but “new” and
“edit” don’t. The current people_controller is:

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

@people_skills = PeopleSkill.all

@people_skill = PeopleSkill.select('skill').where("{person_id =>

@person.id}")

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @person, :xml => @peopleskill }
end

end

and the people_skills_controller.rb#show is:

def show
@people_skill = PeopleSkill.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @people_skill }
end

end

The people_controller.rb#edit, which doesn’t work, is:

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

@people_skills = PeopleSkill.all

@people_skill = PeopleSkill.select('skill').where("{person_id =>

@person.id}")

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @person, :xml => @peopleskill }
end

end

and the people_skills_controller.rb#edit is:

def edit
@people_skill = PeopleSkill.select(“skill”).where("{person_id =>
@person.id}") #find(params[:person_id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @people_skill }
end

end

and these give an error in the \people_skills_form.html.erb at the
first line:

%= form_for(@people_skill) do |f| %>
<% 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 methodmodel_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!
Barney

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.
Barney

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.
Barney

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 - :frowning:

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.

Barney <bsperlin@…> writes:

@person.id}") #find(params[:person_id])
%= form_for(@people_skill) do |f| %>

Any further help would be appreciated and I know you’ve put more into
this than you need to!
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)

Barney <bsperlin@…> writes:

And a relevant section of index.html.erb is:
<%= @people.each do |person| %>

... <%= person.zip_code %> <%= person.skill_set %> <%= 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:

<%= person.employee_info.position %>

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)

 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-
controller-fat-model

Hi Andrew and Curtis,
I’ve been away for 3 days but have come back to the problem
above.
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
default_scope :order => ‘last_name’
has_one :EmployeeInfo
has_one :CandidateInfo
has_one :EmploymentHistory

validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email, :presence => true
end

class EmployeeInfo < ActiveRecord::Base
belongs_to :person
end

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

def index
@people = Person.all
@employee_infos = EmployeeInfo.all

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @people }
end

end

And a relevant section of index.html.erb is:
<%= @people.each do |person| %>

... <%= person.zip_code %> <%= person.skill_set %> <%= WHAT GOES HERE TO BE ABLE TO PRINT THE "position" FIELD OF THE employee_infos TABLE? WHAT CHANGES SHOULD BE MADE TO THE ABOVE?
 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?
Thanks again,
Barney