0

我做了一个应用程序来显示另一个表中的列,但没有工作

这是我的桌子

|policies|
 |id|  |num_policy|

|insurances|
 |id|  |id_policy| |net_insurance| 

这是我的控制器

class PolicyController < ApplicationController
    def generate_print
      @policies= Policy.find(:all)
    end
end

这是我的模型

class Policy < ActiveRecord::Base
  has_many :insurances
end

class Insurance < ActiveRecord::Base
  belongs_to :policy
  has_many :insurance_financing_details
end

class InsuranceFinancingDetail < ActiveRecord::Base
  belongs_to :insurance
end        

这是我的看法

<% @policies.each do |policy| %>
  <%= policy.num_policy %>
  <%= policy.insurance.net_insurance %>
<% end %>

这是我的错误

undefined method `insurance'

我也试过 <%= policy.insurances.net_insurance %>

undefined method `net_insurance'

请有人可以帮我解决这个问题

我将非常感谢帮助

4

1 回答 1

1

Each Policy has many insurances, i.e. an array of them. To get the net_insurance of the first one:

<% @policies.each do |policy| %>
  <%= policy.insurances.first.net_insurance %>
<% end %>

To print all:

<% @policies.each do |policy| %>
  <% policy.insurances.each |insurance| %>
    <%= insurance.net_insurance %>
  <% end %>
<% end %>
于 2013-10-16T22:51:57.313 回答