small medium large xlarge

Generic-user-small
04 Apr 2009, 14:56
Marc Gunton (8 posts)

Essentially, I now have working code to display current stock levels of a product on the main page and show also text saying the item is in stock or out of stock which is displayed next to that number.

I now have only one issue. That is that all I need to do now is get that number and therefore the instock field in the products table to decrement when the place order button is pressed or increment if an order is cancelled via the maintanance page.

Does anyone know how to do this?

Many thanks,

Marc

Samr_small_pragsmall
05 Apr 2009, 07:54
Sam Ruby (634 posts)

The place order button is in a form which is associated with an action named :save_order. In app/controllers/store_controller.rb, there is a method named save_order. If you want an action to be taken (e.g., iterating over the order and decrementing stock counts) whenever the place order button is button is pressed, this would be a place to do it.

Similarly look into the destroy method in app/controllers/orders_controller.rb.

An alternate approach is to use ActiveRecord callbacks, as described in section 20.2.

Generic-user-small
06 Apr 2009, 15:52
Marc Gunton (8 posts)

Hi there!

So would the following code inserted into order.rb be correct:

def before_save self.decrement_instock ||= product.instock -1 end

You must be logged in to comment