Rails - 案例监控应用程序 - 想要在视图中显示来自2个表的数据

时间:2015-05-21 04:20:52

标签: ruby-on-rails

型号:

def index
  @cases = Case.all
end

控制器

<% @cases.each do |c| %>
  <tr>
    <td><%= c.employee.name %></td>
    <td><%= c.problem %></td>
    <td><%= c.assigned_id %></td>
    <td><%= c.solution %></td>
    <td><%= c.verified_id %></td>
  </tr>
<% end %>

查看

$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);

如何让视图显示拥有assigned_id的员工姓名? (我已经使用employee.name来显示input_id列的名称了吗?)

1 个答案:

答案 0 :(得分:0)

使用两个反射

class Case < ActiveRecord::Base
  belongs_to :employee, :foreign_key => :input_id
  belongs_to :assigned_employee, :foreign_key => : assigned_id, : class_name => "Employee"
end

在视图中

<% @cases.each do |c| %>
<tr>
  <td><%= c.employee.name %></td>
  <td><%= c.assigned_employee.name %></td>