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-s开发者_如何学Cubscribes' 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.
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
精彩评论