Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails-更改哈希中的价格值_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails RubyonRails-更改哈希中的价格值

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`") 或 类产品

我通过活动reocrd获得所有产品

@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