0

我是 ruby​​ on rails 的菜鸟,所以我想做的是将两列中每一行的值相乘并将其存储在一个新对象中。我知道如何在 mysql 中执行此操作,但是当我在 .erb 文件中执行此类操作时

@ordertotals = Orderline.where(:id =>  @orderparts.id).sum("quantity * price")

我收到这个错误

undefined method `where' for #<Class:0x7f843ff72ce8>

我一直在环顾四周,找不到关于如何实际进行我理解的位置搜索的解释。我已经看到有些东西实际上需要放入模型文件中,但我不知道从哪里开始如何在模型中定义它或调用它从 .erb 文件中传递所需的信息,有人可以向我解释一下吗?

class Orderline < ActiveRecord::Base

belongs_to :order
end
4

2 回答 2

2

where仅在 Rails 3.x 中添加。由于您使用的是 Rails 2.3.x,因此您必须使用旧语法。

@ordertotals = Orderline.find(:all, :conditions => ["id = ?", @orderparts.id], :select => "quantity * price as total_price")

我希望我的语法正确。没有 rails 2 来测试它。

于 2013-11-07T04:32:30.407 回答
0

Rails 2.3.x 的语法

@ordertotals = Orderline.all(:conditions=>"id='#{@orderparts.id}'",:select=>"quantity * price as PRODUCT")
于 2013-11-07T04:24:30.637 回答