Reputation: 293
I have a database of people that details a person's status within the structure (i.e. boss, tech, student). For each of there people I am generating a show page that gives that person's details. I have set up a way to list a boss's underlings on their show page via a boss_id.
I would like to show the boss's information on the underling show page, but my brain is not connecting these dots.
person.rb
class Person < ActiveRecord::Base
has_many :pubs
default_scope { order('lname') }
belongs_to :boss, class_name: 'Person'
has_many :subordinates, class_name: 'Person', foreign_key: 'boss_id'
end
show.html.erb
<div class="row">
<div>
<% if @person.search == true %>
<div>
<h4><%= @person.prefix %> <%= @person.fname %> <%= @person.lname %> is currently accepting new graduate students for the upcoming Fall semester.</h4>
</div>
<% end %>
</div>
<div id="person_left_show" class="col-sm-6">
<div class="profile">
<% unless Rails.application.assets.find_asset("profiles/#{@person.user_name}.jpg") %>
<%= image_tag("profiles/default.gif") %>
<% else %>
<%= image_tag("profiles/#{@person.user_name}.jpg") %>
<% end %>
</div>
</div>
<p> </p>
<div id="person_right_show" class="col-sm-6 col-md-5 profile_info">
<h2><%= @person.prefix %> <%= @person.fname %> <%= @person.lname %></h2>
<h4><%= @person.title %></h4>
<dl>
<dd><%= @person.position.capitalize %></dd>
<dd><%= @person.school %></dd>
<dd><%= @person.building %> <%= @person.office %></dd>
<dd><%= @person.phone %></dd>
<dd><%= @person.email %></dd>
</dl>
</div>
</div>
<div class="row">
<div class="col-sm-6 profile_desc">
<p> </p>
<dl>
<% if @person.interest.present? %>
<dt>Interest</dt>
<dd><%= @person.interest %></dd>
<% end %>
<% if @person.research.present? %>
<dt>Current Research</dt>
<dd><%= @person.research %></dd>
<% end %>
<% if @person.pubs.any? %>
<dt>Recent Publications</dt>
<dd>
<table class="table-striped">
<tbody>
<% @person.pubs.each do |pub| %>
<tr>
<td><%= pub.authors %>, <%= pub.title %>, <%= pub.journal %>, <%= pub.date %>
<% if pub.link.present? %>
<%= link_to 'PubMed', (pub.link), :target => "_blank" %>
<% end %>
<% if pub.file.present? %>
<%= link_to ' | PDF', asset_path("pubfiles/#{pub.file}"), :target => "_blank" %>
<% end %>
</td> </tr>
<% end %>
</tbody>
</table>
</dd>
<% end %>
<% if @person.pubmed.present? || @person.scholar.present? || @person.sci_index.present? %>
<p>
<h4>All Publications
<% if @person.pubmed.present? %>
<%= link_to image_tag("pubmed.png", size: "24", alt: "PubMed"), (@person.pubmed), :target => "_blank", :title => "PubMed" %>
<% end %>
<% if @person.scholar.present? %>
<%= link_to image_tag("scholar.png", size: "24", alt: "Google Scholar"), (@person.scholar), :target => "_blank", :title => "Google Scholar" %>
<% end %>
<% if @person.sci_index.present? %>
<%= link_to image_tag("sci.png", size: "24", alt: "Science Citation Index"), (@person.sci_index), :target => "_blank", :title => "Science Citation Index" %>
<% end %>
</h4>
</p>
<% end %>
</dl>
</div>
<div class="col-sm-6">
<% if @person.labweb.present? %>
<p><h3><%= link_to 'Lab Website', (@person.labweb), :target => "_blank" %></h3></p>
<% end %>
<% if @person.grantlink.present? %>
<p><h3><%= link_to 'Training Grant Opportunities', (@person.grantlink), :target => "_blank" %></h3></p>
<% end %>
<!--Lab Links and people go here. -->
<% if @person.subordinates.any? %>
<h3>Lab Members</h3>
<table>
<% @person.subordinates.each do |sub| %>
<tr>
<td>
<p>
<% if sub.position == 'grad' %>
<%= link_to(friendly_person_path(sub.position, sub.user_name)) do %>
<strong><%= sub.fname %> <%= sub.lname %></strong><% end %>, Graduate Student
<% elsif sub.position == 'postdoc' %>
<%= link_to(friendly_person_path(sub.position, sub.user_name)) do %>
<strong><%= sub.fname %> <%= sub.lname %></strong><% end %>, Postdoctoral Associate
<% elsif sub.position == 'nn-grad' %>
<%= link_to(sub.labweb) do %>
<strong><%= sub.fname %> <%= sub.lname %></strong><% end %>, Graduate Student - <%= sub.school %>
<% elsif sub.position == 'nn-postdoc' %>
<%= link_to(sub.labweb) do %>
<strong><%= sub.fname %> <%= sub.lname %></strong><% end %>, Postdoctoral Associate - <%= sub.school %>
<% elsif sub.position == 'labman' %>
<%= link_to(friendly_person_path(sub.position, sub.user_name)) do %>
<strong><%= sub.fname %> <%= sub.lname %></strong><% end %>, Lab Manager
<% elsif sub.position == 'labtech' %>
<%= link_to(friendly_person_path(sub.position, sub.user_name)) do %>
<strong><%= sub.fname %> <%= sub.lname %></strong><% end %>, Lab Tech
<% else %>
<%= link_to(friendly_person_path(sub.position, sub.user_name)) do %>
<strong><%= sub.fname %> <%= sub.lname %></strong><% end %>, <%= sub.position.capitalize %>
<% end %>
</p>
</td>
</tr>
<% end %>
</table>
<% end %>
</div>
</div>
</div>
With help of how to just set up the call for the boss's person.lname, then I will be able to get the rest.
Upvotes: 1
Views: 30
Reputation: 186
You should be able to just call @person.boss.lname
in order to return the boss's last name.
Upvotes: 1