Rails数组如何将列与数字相乘

时间:2011-12-21 00:10:12

标签: ruby-on-rails ruby ruby-on-rails-3

这是我的Taletids表:

Price (integer)
Price2(integer) 

在我看来,我有:

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

但我想将价格列乘以2.

@taletids数组总和中添加一个“假”列,其中Price2乘以2 (params[:tal])和价格列。

因此,我可以在视图中调用总和:

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

2 个答案:

答案 0 :(得分:2)

您可以在代表总和的Taletids模型中添加一个方法:

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

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

答案 1 :(得分:1)

如果我正确理解你,你可以为你的Taletid模型(app / models / taletid.rb)添加一个方法来进行你想要的计算。

def sum
    (price2 * 2) + price
end

希望有所帮助。