Ruby on Rails如何从视图中打印联接表?

时间:2019-03-24 13:21:31

标签: ruby-on-rails-4

我正在尝试在Ruby on Rails中创建案例管理系统。我有两个表:audit_cases和ra_cases。 “ audit_cases”具有“ Audit_Charge_Code”(主键)和“ Client_ID”列。 “ ra_cases”具有“ audit_case_id”(外键),“ RA_Charge_Code”,“ RA_Manager”,“ RA_IC”,“ RA_Performer”等列。

我已经加入了两个表

型号:

    class RaCase < ActiveRecord::Base
        belongs_to :audit_case 
    end

    class AuditCase < ActiveRecord::Base
        self.primary_key = "Audit_Charge_Code"
        has_many :ra_cases 
    end

控制器:

    def index
            @a_cases = AuditCase.includes(:ra_cases).all
    end

视图:

    <% @a_cases.each do |a_case| %>
          <tr>
            <td><%= a_case.Client_ID %></td>
            <td><%= a_case.Audit_Charge_Code %></td>
            <td><%= a_case.ra_cases.each do |ra_case| %>
              <%= ra_case.RA_Charge_Code %> <%= ra_case.RA_Performer %><br>
              <% end %>
            </td>
          </tr>
    <% end %>

我希望输出为:

    ClientID    AuditChargeCode RAChargeCode
    PCC502  PCC5020218  PCC5027119
    PCC502  PCC5020218  PCC5027120
    PCC502  PCC5020218  PCC5027121

但是实际输出是:

    ClientID    AuditChargeCode RAChargeCode
    PCC502  PCC5020218  PCC5027119
                        PCC5027120
                        PCC5027121
                        [#<RaCase id: 1, audit_case_id: "PCC5020218", RA_Charge_Code: "PCC5027119", RA_Manager: "7829", RA_IC: "7687", RA_Performer: "7572", case_type: "GITC", fee: 75000, created_at: "2019-03-21 13:57:03", updated_at: "2019-03-21 13:58:19">, #<RaCase id: 2, audit_case_id: "PCC5020218", RA_Charge_Code: "PCC5027119", RA_Manager: "7829", RA_IC: "7687", RA_Performer: "7572", case_type: "AP Control", fee: 35000, created_at: "2019-03-21 19:01:07", updated_at: "2019-03-21 19:45:08">, #<RaCase id: 3, audit_case_id: "PCC5020218", RA_Charge_Code: "PCC5027119", RA_Manager: "7829", RA_IC: "7687", RA_Performer: "7408", case_type: "GITC", fee: 75000, created_at: "2019-03-21 19:14:41", updated_at: "2019-03-21 19:45:47">]

我想知道如何才能不打印整个数据数组[#<...>]。

非常感谢您!

1 个答案:

答案 0 :(得分:0)

打印整个数据数组的原因是因为您使用的是<%= %>格式。此格式打印其结果。您将需要使用<% %>格式,该格式不会显示其结果:

<% ra_case.RA_Charge_Code %> <%= ra_case.RA_Performer %>