# Stock

**URL:** <https://www.ruby-forum.com/t/stock/105323>\
**Category:** Rails\
**Created:** [May 28, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/stock/105323 "2007-05-28T20:46:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gino](https://www.ruby-forum.com/u/gino)\
**Post date:** [May 28, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/stock/105323/1 "2007-05-28T20:46:47Z")

</div>

is there an easy way, or hard way, to have amount of items, and as  
they’r been sold the amount or quantity be subtructed, and as they are  
added, the amount or quantity grows, ? I can see that everybody wants to  
sell but nobody wants to know whether there’s avalibale items, I guess  
items are endless in almost everybody’s world, but not mine, i work with  
items that do finish and sometiems I never have them again,  
Ok it’s like inventory thing  
It’s just, that there’s no information avalibale that I know of  
almost all the manuals, and books, the example application is about  
selling books  
is that the only thing this world sells? I say this because books  
probably don’t have the problem of aviability, so they donn’t show you  
how to keep track of your items  
ok, It’s probably my lack of skill, to know how to do it, I’ll just have  
to keep practicing, because I’m very new at web or anything related with  
sofware  
if someome knows where I can get iformation about it, I’ll apriciate it,  
if not, then I’ll have to be on my own, it feels so frightening

---

<div class="post-metadata">

**Author:** ![gino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@gino](https://www.ruby-forum.com/u/gino)\
**Post date:** [May 28, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/stock/105323/2 "2007-05-28T22:03:10Z")

</div>

Problems like this are exactly why people use programming languages to  
“program” software.

This is no different than the book examples you have read – you just  
want  
to also have the concept of stock levels.

This is applicable to the book world as well. Amazon’s book selling  
system  
is aware of its stock levels. Powell’s is aware of its stock levels.  
However, unless they are really cool, the Paperback Book Exchange store  
down  
the street doesn’t.

What do you do?

Create a StockLevel model, that will “belong to” your Product class (in  
the  
case of the book store it might just be a Book class… if the types of  
products you are going to offer are all going to be different “models”  
then  
you’ll probably want to use polymorphic associations for your stock  
levels  
instead of a simple association).

Make a controller for your vendor to add stock to the product ( each row  
of  
stock level would have a float column with positive or negative  
allocations  
of stock made. )

When someone purchases one or more products, add an instance to your  
StockLevel model with the number the customer bought as a negative.

In your store logic, you can choose to show or not show your Products  
based  
on their availability.

This is one way to solve the problem with or without Rails. Rails though  
will certainly make this dead simple with very clean and readable code.  
You’ll find yourself building methods like Product#add\_stock(5),  
Product#subtract\_stock(1), Product#current\_stock\_level (which would just  
do  
a simple product.stock\_levels.calculate(:sum, :level) ), etc.

Hope this helps you conceptualize what you’ve got in front of you.

D. Taylor S.,  
Reality Technician  
[http://realitytechnicians.com](http://realitytechnicians.com)
