Pregunta

I did an app to show a column from another table but is not working

Here my tables

|policies|
 |id|  |num_policy|

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

This is my controller

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

This is my model

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        

This is my view

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

Here is my error

undefined method `insurance'

i also tried <%= policy.insurances.net_insurance %>

undefined method `net_insurance'

Please somebody can help me with this problem

I will really appreciate help

¿Fue útil?

Solución

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 %>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top