【发布时间】:2011-07-27 04:11:23
【问题描述】:
所以我有以下型号:
- 用户
- 商务
- 优惠券
- 特惠
- 可兑换
- 所有者
我正在使用所有者模型将各种其他模型(例如商业优惠券交易和可兑换)关联到用户模型。
使用可兑换模型是因为我想为使用优惠券/交易的每个用户生成一个唯一代码,以限制每个用户使用一个以及总金额。
我想也许我不需要两种用于优惠券和交易的模型,我只需要一个,因为交易更像是一种特殊类型的优惠券。
以下是修改后的模型:
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 => true
对于用户模型,我完全迷失了 但这就是我想要的伪代码 仅针对所有可能遇到相同问题的人进行编辑,这是我设置用户模型的方式
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'
我只是不明白如何将优惠券与用户模型相关联,因为我为拥有东西做了多态关联。
【问题讨论】:
-
考虑到可赎回是一个形容词,这会将其作为对象的属性或表格的列。作为动词(赎回),它将由关联表示。你把它作为一个实体(对象),所以这里有些东西是关闭的。或许您可以描述您尝试建模的过程?
标签: ruby-on-rails ruby-on-rails-3 database-design activerecord