[英]Ruby on Rails - change value of the price in Hash
我正在通過Active reocrd獲取所有產品
@results = Product.all
現在,我想將每個“價格”值乘以“價格* 100”,該怎么做?
@results = Product.all.select("*, `price` * 100 AS `price`")
要么
class Product < ActiveRecord::Base
def price
super * 100
end
end
要么
class Product < ActiveRecord::Base
def cents
price * 100
end
end
將虛擬屬性添加到app / models / product.rb
class Product < ActiveRecord::Base
...
def price_in_cents # or some other appropriate name
price * 100
end
...
end
並將其用作@ results.first.price_in_cents之類的東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.