# Sale, pop and product

**URL:** <https://www.ruby-forum.com/t/sale-pop-and-product/168975>\
**Category:** Rails\
**Created:** [May 28, 2009, 12:29am UTC](https://www.ruby-forum.com/t/sale-pop-and-product/168975 "2009-05-28T00:29:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [May 28, 2009, 12:29am UTC](https://www.ruby-forum.com/t/sale-pop-and-product/168975/1 "2009-05-28T00:29:16Z")

</div>

Hi,  
I couldn’t find a better word for pop, but let’s go:  
There are 3 models: sale, pop and product

sale:  
-total:integer  
pop:  
-quantity:integer  
-discount:float  
product:  
-name:string  
-price:float

A sale can have many different products, each one has a quantity and  
discount associated.  
So each sale has\_many :products through =\> pops  
And each product has\_many :sales through =\> pops

For example:  
±------------------------------------------+  
|–\> Sale 1 \<–\> Pop 1 \<–\> Product 1 |  
| qty = 20 |  
| disc= 0.1 |  
| \<–\> Pop 2 \<–\> Product 2 |  
| qty = 10 |  
| disc=0 |  
±------------------------------------------+  
|–\> Sale 2 \<–\> Pop 3 \<–\> Product 3 |  
| qty = 4 |  
| disc= 0.5 |  
| \<–\> Pop 4 \<–\> Product 1 |  
| qty = 5 |  
| disc=0 |  
±------------------------------------------+

By the time I associate a sale with a product I need to especify  
quantity and discount because Sale’s model has validations that uses  
quantity and discount values.

In rails console:  
s = Sale.new  
p = Product.new  
p.save  
s.products \<\< Product.last  
s.save

This way I can’t use pops columns before saving the sale. How can I do  
that?

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [May 28, 2009, 12:47am UTC](https://www.ruby-forum.com/t/sale-pop-and-product/168975/2 "2009-05-28T00:47:57Z")

</div>

Not sure how helpful my comments will be, but…

Sale = Order  
Pop = LineItem  
Product = Product

An Order has many LineItems. A LineItem belongs to an Order. A  
LineItem has one Product.

The LineItem model should have fields for quantity, discount, and the  
price of the product at the time the order was placed.

The Order model should only need to look at it’s LineItems to  
determine the total price.

Don’t know if that will help work your way through the issue or not…  
if not, ignore me 🙂

-philip

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [May 28, 2009, 6:08am UTC](https://www.ruby-forum.com/t/sale-pop-and-product/168975/3 "2009-05-28T06:08:36Z")

</div>

Thanks for your help!

I got this code:

1. s = Sale.new
2. p = Product.new
3. p.save  
pop = s.pops.build(:qty =\> 10, :disc = 0) #associates pop with sale and  
puts it in the collection
4. pop.product = p
5. s.save #saves sale and pop

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [May 28, 2009, 6:30am UTC](https://www.ruby-forum.com/t/sale-pop-and-product/168975/4 "2009-05-28T06:30:34Z")

</div>

Just found out that “pops” callbacks simply do not work.  
for example:  
class Sale  
has\_many :pops, :dependent =\> :destroy  
end

class Pop  
before\_destroy :do\_something  
end

if I destroy a sale that has a pop associated, before\_destroy from Pop  
is not called.

why that happens?

---

<div class="post-metadata">

**Author:** ![steux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@steux](https://www.ruby-forum.com/u/steux)\
**Post date:** [June 9, 2009, 5:56am UTC](https://www.ruby-forum.com/t/sale-pop-and-product/168975/5 "2009-06-09T05:56:46Z")

</div>

Any ideas how can this be used in new/create views?

Bruno S. wrote:

> Thanks for your help!
> 
> I got this code:
> 
> 1. s = Sale.new
> 2. p = Product.new
> 3. p.save  
> pop = s.pops.build(:qty =\> 10, :disc = 0) #associates pop with sale and  
> puts it in the collection
> 4. pop.product = p
> 5. s.save #saves sale and pop
