Rails 4从同一个表中提取数据

时间:2016-07-08 17:34:53

标签: ruby-on-rails ruby-on-rails-4

我有一个人员数据库,详细说明了人在结构中的状态(即老板,技术,学生)。对于每个人,我正在生成一个显示该人的详细信息的节目页面。我已经建立了一种方法,通过boss_id在他们的节目页面上列出老板的下属。

我想在下属节目页面上显示老板的信息,但我的大脑并没有连接这些点。

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>

在如何为老板的person.lname设置呼叫的帮助下,我将能够得到其余的。

1 个答案:

答案 0 :(得分:1)

您应该只需致电@person.boss.lname即可返回老板的姓氏。