0

4 つのモデルのスキーマは次のようになります: http://pastie.org/1576759

計画テーブルには、実際の計画に関するすべてのデータが格納されます。サブスクリプションには、ユーザーがサービスに「再サブスクライブ」した月が保存されます。トランザクションには、支払い関連の情報が保存されます。

モデル間の関連付けはどのように機能しますか?

例:ユーザー :belongs_to plan, :through => :subscription ?

サブスクリプション "has_many" :plans ?

Rails とアソシエーションに関して、これらすべてがどのように結びついているかについては、少し曖昧です。

4

1 に答える 1

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
于 2011-02-18T03:32:41.743 に答える