Reputation: 134
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
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
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