2011-02-18 16 views
0

以下是四種模型的模式:http://pastie.org/1576759訂閱/循環計費Rails應用程序中的模型之間的關聯是什麼?

計劃表存儲實際計劃的所有數據。訂閱存儲每個月用戶'重新訂閱'的服務。交易存儲與付款相關的信息。

協會如何在模型之間工作?

E.g.用戶:belongs_to計劃,:通過=>:訂閱?

訂閱「has_many」:計劃?

對於Rails和協會如何將這一切聯繫在一起,我有些模糊。

+0

爲什麼你的用戶和訂閱表中有plan_id? – 2012-01-29 18:29:12

回答

3
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 
相關問題