# Designing a Coupon System

**URL:** <https://www.ruby-forum.com/t/designing-a-coupon-system/96179>\
**Category:** Rails\
**Created:** [June 27, 2006, 1:24am UTC](https://www.ruby-forum.com/t/designing-a-coupon-system/96179 "2006-06-27T01:24:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [June 27, 2006, 1:24am UTC](https://www.ruby-forum.com/t/designing-a-coupon-system/96179/1 "2006-06-27T01:24:56Z")

</div>

I’m racking my brain trying to figure out a good method for a coupon  
system for our ecommerce site. Basically We have a variety of  
consditions for certain coupons and a variety of effects.

Some examples:

1. 5% off any order
2. Free shipping on orders over $50
3. Free shipping on an order if a product foo is part of the order
4. 10% off product foo
5. 5% off entire order if product foo is in the order

So I need to somehow store the properties of a coupon, and validate that  
all its conditions are met, and then apply a couple different effects.  
I’m a bit baffled about where to go next. I thought of creating a Proc  
for conditions and serializing it in the DB, but that makes it hard to  
define via an interface and is just plain messy.

Any ideas on how to approach this type of polymorphic complexity?

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [June 27, 2006, 1:46am UTC](https://www.ruby-forum.com/t/designing-a-coupon-system/96179/2 "2006-06-27T01:46:04Z")

</div>

Well where a particular product (foo) is involved, that information  
should probably be included in the products table (eg, two fields:  
discount, order\_discount). Any other discount should maybe be included  
in a discounts table, with some fields representing conditions  
(order\_greater\_than), and some representing the results if those  
conditions are met (shipping\_discount). You should probably have a  
bool to represent whether it can be used in conjunction with other  
offers too.

Actually, I’ve just thought, for the product foo, you should probably  
have a product\_discount\_group table with the two fields above which  
:has\_many =\> products. That would make it easier to put products into  
discount groups.

That’s the best thing I can think of. I’d be interested to see other  
suggestions.

-Nathan
