ruby - Adding and Accessing Multiple Session Variables in Rails -


i need way change status of products in store sold once purchase completed...

i use following code:

if @order.purchase      #i use active merchant's purchase method on compiled order   update_product_status #method explained below end 

before product copied line_item code run:

@product = product.find(params[:product_id]) #line_item belongs product , cart session[:pending_purchase] = @product.id     #stores id number session  

so later can pull id, update_product_status code:

def update_product_status              #used update boolean 'sold' status on product   product = session[:pending_purchase]   @sold_product = product.find(product)   @sold_product.update(:sold = true)   #not sure if syntax correct end 

a problem may present if buys 2 items. id in :pending_purchase on written if second line_item created , added cart? how access both variables , make sure both items have 'sold = true' attribute?

the simplest way stores product's id array.

@product = product.find(params[:product_id]) (session[:pending_purchase] ||= []) && (session[:pending_purchase] << @product.id ) 

and find more products when use it:

   @products = product.find(session[:pending_purchase])    @products.each |p|      .........    end 

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 -