Search code examples
ruby-on-rails-3subscriptionrecurring-billing

What are the associations between the models in a subscription/recurring billing Rails app?


This is how the schema for the four models look: http://pastie.org/1576759

The plan table stores all the data on the actual plans. The subscription stores each month that a user 're-subscribes' to the service. The transaction stores the payment related info.

How would the associations work between models ?

E.g. a user :belongs_to plan, :through => :subscription ?

A subscription "has_many" :plans ?

I am a bit fuzzy on how this all ties together with respect to Rails and associations.


Solution

  • 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