What's the best way to model these database associations?
So I have the following models:
- User
- Business
- Coupons
- Deals
- Redeemable
- owner
I am using the owner model to associate various other models such as business coupons deals and redeemable to the user model.
The redeemable model is used because I want to generate a unique code for each user that uses a coupon/deal in order to limit one per user as well as the amount in total.
I suppose maybe I don't need two models for coupon and deal and I just need one because a deal is more like a special type of coupon.
So heres the models after a revision:
Business
has_many :owners, :as => :ownable, :dependent => :destroy
has_many :coupons, :dependent => :destroy
Coupon
belongs_to :business
has_many :redeems
# also has a special column for denoting weather it's a
# normal coupon or a daily deal kind of coupon
Redeem
belongs_to :coupon
has_one :owner, :as => :ownable, :dependent => :destroy
Owner
belongs_to :user
belongs_to :ownable, :polymorphic => 开发者_如何学JAVAtrue
and for the user model I am just totally lost but here's what I want in pseudo code edit just for all those who may have same problem here's how i set up the user model
User
has_many :owners
has_many :businesses, :through => :owners,
:source => business, :source_type => 'Business'
has_many :redeems, :through => :owners, :source=> :redeem,
:source_type => 'Redeem'
has_many :coupons, :through => :redeems,
:source => :coupon, :source_type => 'Coupon'
I just don't understand how to associate the coupons with the User model because I did a polymorphic association for owning things.
I would say that a coupon is essentially a contract, a certificate, an instance of a deal. When a person buys a coupon, they actually buy a legal contract that some business will deliver on a promised product or service described in the contract (deal). So with that in mind, here is an attempt to model this.
精彩评论