【问题标题】:What are the associations between the models in a subscription/recurring billing Rails app?订阅/定期计费 Rails 应用程序中的模型之间有什么关联?
【发布时间】:2011-06-29 12:41:37
【问题描述】:
四个模型的架构如下所示:http://pastie.org/1576759
计划表存储有关实际计划的所有数据。订阅存储用户“重新订阅”服务的每个月。交易存储支付相关信息。
模型之间的关联如何运作?
例如用户 :belongs_to plan, :through => :subscription ?
订阅“has_many”:计划?
对于 Rails 和关联如何将这一切联系在一起,我有点模糊。
【问题讨论】:
标签:
ruby-on-rails-3
subscription
recurring-billing
【解决方案1】:
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :plan
end
class User < ActiveRecord::Base
belongs_to :plan
has_many :subscriptions (or has_one, if a user only has 1 subscription at a time)
has_many :transactions
end
class Transaction < ActiveRecord::Base
belongs_to :user
belongs_to :plan
end
class Plan < ActiveRecord::Base
has_many :subscriptions
has_many :users
end