small medium large xlarge

24 Mar 2009, 14:24
Marc Gunton (8 posts)

Hi guys,

I’m doing ruby on rails for my final year project at university and was looking at ways to improve the depot application.

Can anyone point me in the right direction for adding a stock count feature to the application so that if there are no items in stock it’ll display out of stock, else display the number of that item currently in stock.

How would I add a quantity field to the database and which databases would need to be affected?

Cheers guys

27 Mar 2009, 23:55
Richard Delph (8 posts)

i would think you simply create a migration e.g. ‘ruby script/generate migration add_quantity_to_products quantity:number’ and amend the view code to display either the number of items in stock or write a function in the product model which displays stock text e.g.

def display_stock
  if stock == 0 then
    "Out of Stock"
    "In Stock"

and amend the view to call that? remember that you have to amend your checkout process to reduce the stock and perform any checks necessary, also when adding to cart they can’t add a number greater than the stock amount or they can if the store has a back-order facility. generally stock control in an ecommerce app is one of the biggest areas to tackle from experience. you might have a column called quantity or stock in the products table but it’s only reflection of the stockhistory table for speed purposes and there’s so much more to it than that to explain here, this doesn’t even scratch the surfact but hope it sets you off in the right direction!

Cheers Rich

31 Mar 2009, 15:35
Marc Gunton (8 posts)

Hi Rich,

Thanks for replying. I see that the code there is pretty simple which is good!!! However, for some reason I’m struggling to get it all to display properly in my store.index.html.erb. I think its because I’m calling the method above incorrectly. This is strange as I managed to get it to display my product.instock field. Below is my code which displays the price and number in stock fields and also the add to cart button…all on the same line in the store.index.html.erb file:

<%=currency_pound product.price -%>
<%=product.instock -%>
<%= button_to "Add to Cart", :action => 'add_to_cart', :id => product %>

Any suggestions??? I tried:

<%=display_stock product.instock -%>

But that only threw an undefined method or local variable error. Also tried popping the method into my application and store helpers but to no avail......


31 Mar 2009, 20:46
Matthew McFarling (1 post)

In the product model you should have:

def display_stock
  (stock == 0) ? "Out of Stock" : "In Stock"

Then in your view if you have access to the product variable which I see that you do because you can do product.price, then you simply do product.display_stock. It will return “Out of Stock” or “In Stock” depending on the column in the products table named stock.

01 Apr 2009, 13:33
Marc Gunton (8 posts)

Hi Matthew,

Great work there. I think I hadn’t declared it correctly in the product model. Anyway, that all works now. Just a couple of minor things to sort now….

Firstly, rather than display 0 as well as out of stock, how would I make the value dissapear so that all that appears is items out of stock? It purely a visual thing but it would just make it look that little bit better!

Also does anyone know what the following flash notice refers to:

translation missing: en, flash, thanks

This pops up when I use checkout. I have a feeling it might be something to do with what installation of flash I have as I’ve recently installed a new update. Oh dear. Even so, the order still goes through without any errors which is strange.

01 Apr 2009, 13:45
Marc Gunton (8 posts)

Also I need to get the stock value to decrement after checking out. I assume this would be linked to the checkout button perhaps???

any ideas??

Cheers for the support guys, much appreciated!!

You must be logged in to comment