0

In my rails app I have a Jobs model. In the Jobs model there are three separate fields in the Job record that I want to sum - cabinetry_cost, counter_top_cost and install_cost.

To do so, I have the following method in my jobs model:

def revenue
  job_by_id = Job.find(params[:id])
  revenue = job_by_id.sum(:cabinet_cost + :counter_top_cost + :install_cost)
end

In my view, I am calling the method as follows (using job.revenue below):

<% @customer.jobs.each do |job| %>
  <tr>
    <td><%= link_to job.id, customer_job_path(@customer, job) %></td>
    <td><%= job.job_tag %></td>
    <td></td>
    <td><%= job.install_date %></td>
    <td><%= job.revenue %></td>
  </tr>
<% end %>

Currently, I am getting the following error:

undefined local variable or method `params'  

Does anyone know what I am missing?

Dustin James
  • 575
  • 9
  • 21

1 Answers1

2

You don't need to sum any thing in your model, just add them.

Class Job <  ActiveRecord::Base
  def revenue
    cabinet_cost + counter_top_cost + install_cost
  end
end

params is the variable came from your view, should be used in your controller, not in your model. You should step back to learn some thing about Rails framework first.

Jaugar Chang
  • 3,176
  • 2
  • 13
  • 23
  • Thanks, this does the trick - and yes, this is my first build and also part of me learning about the Rails framework. If you have any resources that can help with my learning regarding this issue please let me know. – Dustin James Aug 30 '14 at 03:41
  • 1
    Could you not argue the virtues of putting this into a helper? – Richard Peck Aug 30 '14 at 11:53
  • 2
    Sorry @RichPeck ! I just want share my experience and what I found useful on SO with the OP. I'm not good at English, but I'm willing to improve it. Is the sentence of `You should ...` let you think I was arguing the virtues. I use the sentence that I was just learned from the discussions in http://stackoverflow.com/questions/25546314/undefined-method-order-for-array0x26b1c40-in-rails/25546784#25546784. But I forgot to add `a bit` in it, did that bring to the misunderstanding? If so, I'm willing to fix it, or could you suggest some more proper way? – Jaugar Chang Aug 30 '14 at 15:25