How do i append some value to an instance variable in ruby on rails? -
here's trying in home controller:
def view     @product= product.find(params[:id])     rand_price = rand(202- @product.price.to_i) + @product.price.to_i     old_price = rand_price + @product.price.to_i      @product << old_price  #error line end i want add 1 more value of old_price variable without adding column same in product model. error is:
undefined method `<<' #<product:0x7f1362cc5b88> 
 use serializers in model:
class product << activerecord::base   serialize :prices, array    ... end column products.prices in database should string.
and in controller
def update   @product= product.find(params[:id])   rand_price = rand(202- @product.price.to_i) + @product.price.to_i   new_price = rand_price + @product.price.to_i    @product.prices << new_price   @product.save! end and may use it:
  @product.prices # => array of prices.   @product.prices.last # => current price 
Comments
Post a Comment