Jamil Khan
Jamil Khan

Reputation: 134

Ruby on Rails - change value of the price in Hash

I am getting all products through Active reocrd

@results = Product.all

Now I want to multiply every "price" value to "price*100" how do I do that?

Upvotes: 0

Views: 133

Answers (2)

Timo Schilling
Timo Schilling

Reputation: 3073

@results = Product.all.select("*, `price` * 100 AS `price`")

or

class Product < ActiveRecord::Base
  def price
    super * 100
  end
end

or

class Product < ActiveRecord::Base
  def cents
    price * 100
  end
end

Upvotes: 2

roob
roob

Reputation: 1136

Add a virtual attribute to app/models/product.rb

class Product < ActiveRecord::Base
  ...

  def price_in_cents  # or some other appropriate name
    price * 100
  end
  ...
end

And use it as something like @results.first.price_in_cents

Upvotes: 0

Related Questions