Rails beginner
Rails beginner

Reputation: 14504

Rails array how to multiply a column with a number

This is my Taletids table:

Price (integer)
Price2(integer) 

In my view I have:

@taletids = Taletid.where(:online => true).order('position')

But I want to multiply the price column with 2.

And add a "fake" column to the @taletids array sum with is the sum of the Price2 multiplied with 2 (params[:tal]) and the Price column.

So that I can call the sum in view as this:

<% @taletids.each do |tale| %>
  <%= tale.sum %>
<% end %>

Upvotes: 1

Views: 1840

Answers (2)

Matt Hulse
Matt Hulse

Reputation: 6212

You could add a method to your Taletids model representing sum:

class Taletids < ActiveRecord::Base
  def sum
    self.Price + (self.Price2 * 2)
  end

  def sum_x(x)
    self.Price + (self.Price2 * x)
  end
end

Upvotes: 2

Lenny Sirivong
Lenny Sirivong

Reputation: 1031

If I'm understanding you correctly, you can add a method to your Taletid model (app/models/taletid.rb) which does the computation you want.

def sum
    (price2 * 2) + price
end

Hope that helps.

Upvotes: 1

Related Questions