Ruby on rails RubyonRails-更改哈希中的价格值
我通过活动reocrd获得所有产品Ruby on rails RubyonRails-更改哈希中的价格值,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我通过活动reocrd获得所有产品 @results = Product.all 现在我想把每个“价格”值乘以“价格*100” 我该怎么做 @results = Product.all.select("*, `price` * 100 AS `price`") 或 类产品
@results = Product.all
现在我想把每个“价格”值乘以“价格*100”
我该怎么做
@results = Product.all.select("*, `price` * 100 AS `price`")
或
类产品
或
类产品
将虚拟属性添加到app/models/product.rb
class Product < ActiveRecord::Base
...
def price_in_cents # or some other appropriate name
price * 100
end
...
end
类产品
并将其用作@results.first.price(单位:美分)
class Product < ActiveRecord::Base
def cents
price * 100
end
end
class Product < ActiveRecord::Base
...
def price_in_cents # or some other appropriate name
price * 100
end
...
end