【问题标题】: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 和关联如何将这一切联系在一起,我有点模糊。

【问题讨论】:

  • 为什么你的用户和订阅表中有plan_id?

标签: 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

【讨论】:

    猜你喜欢
    • 2011-06-28
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多