1

I did an application using rails and I'm having problems doing a total sum on each row but i'm getting errors.

Here my tables:

|policies|
   |id|  |num_policy|
    1       1000
    2       1001
    3       1002
    4       1003

|policy_vehicles|    
   |id|  |policy_id|  |vehicle_id|
    1         1            1
    2         1            2
    3         2            1
    4         2            3
    5         3            1
    6         4            4
    7         4            2

|vehicles|
   |id|  |name|     |amount|
    1    VEHICLE1     8000
    2    VEHICLE2     8001
    3    VEHICLE3     8002
    4    VEHICLE4     8003

Here is my controller: "app/controllers/policy_controller.rb"

Models:

class PolicyVehicle < ActiveRecord::Base
   belongs_to :vehicle
   belongs_to :policy
end

class Vehicle < ActiveRecord::Base
   belongs_to :policy
   has_many :policy_vehicles
end

class Vehicle < ActiveRecord::Base
   has_many :policy_vehicles
end

Here is my index view: "app/views/policy/index.html.erb"

<table border="1">
  <tr>
    <td>POLICY ID</td>
    <td>NUM POLICY</td>
  </tr>

<% @policies.each do |policy| %>
  <tr>
    <td><%= policy.id %></td>
    <td><%= policy.num_policy %></td>
  </tr>

  <% policy.policy_vehicles.each do |policy_vehicle| %>
  <tr>
    <td></td>
    <td><%= policy_vehicle.vehicle.name %></td>
    <td><%= policy_vehicle.vehicle.amount %></td>
  </tr>
  <% end %>

  <tr>
    <td></td> 
    <td>TOTAL</td>
    <td><%= policy.policy_vehicles.vehicles.sum(:amount) %></td>
  </tr>

<% end %> 
</table>

If I try:

<%= policy.policy_vehicles.vehicles.sum(:amount) %>
##### GET THIS ERROR  #####
undefined method `vehicles' for #<Class:0x7ff4293ac3f0>

If I try:

<%= policy.policy_vehicles.vehicle.sum(:amount) %>
##### GET THIS ERROR  #####
undefined method `vehicle' for #<Class:0x7ff42ae95a90>

If I try:

<%= policy.policy_vehicles.vehicles.collect(&:amount).sum %>
##### GET THIS ERROR  #####
undefined method `vehicles' for #<Class:0x7ff429337410>

If I try:

<% policy.policy_vehicles.each do |policy_vehicle|%> 
  <%= policy_vehicle.vehicle.sum(:amount)
<% end %>
##### GET THIS ERROR  #####
undefined method `sum' for #<ActiveRecord::Associations::BelongsToAssociation:0x7ff4295dbd60>

Finally I tried:

<% policy.policy_vehicles.each do |policy_vehicle|%> 
  <%= policy_vehicle.vehicle.amount.sum
<% end %>
##### GET THIS ERROR  #####
undefined method `sum' for 8000:Fixnum

I'm trying to do this example:

enter image description here

Please somebody can help me?

What is wrong?

All kind of help will be accepted.

1 Answer 1

5

You could do:

class Policy < ActiveRecord::Base
   has_many :policy_vehicles
   has_many :vehicles, through: policy_vehicles
end

and then:

<tr>
  <td></td> 
  <td>TOTAL</td>
  <td><%= policy.vehicles.sum(:amount) %></td>
</tr>
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic +1 for excellent answer, notice that also I wrote good details and explained as good question :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.