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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -