这是我的达人表:
Price (integer)
Price2(integer) 在我看来,我有:
@taletids = Taletid.where(:online => true).order('position')但是我想把价格列乘以2。
并向@taletids数组添加一个"fake“列sum with是Price2乘以2 (params[:tal])和Price列的和。
这样我就可以像这样调用视图中的总和:
<% @taletids.each do |tale| %>
<%= tale.sum %>
<% end %>发布于 2011-12-21 08:40:00
您可以将一个表示sum的方法添加到您的Taletids模型中:
class Taletids < ActiveRecord::Base
def sum
self.Price + (self.Price2 * 2)
end
def sum_x(x)
self.Price + (self.Price2 * x)
end
end发布于 2011-12-21 08:39:01
如果我没理解错的话,你可以在你的Taletid模型(app/models/talentid.rb)中添加一个方法来完成你想要的计算。
def sum
(price2 * 2) + price
end希望这能有所帮助。
https://stackoverflow.com/questions/8583619
复制相似问题