我有以下型号。
我不希望User
可以多次订阅同一个App
所以这应该是不可能的:
User.last.apps
=> [#<App id: 78, name: "Name">, #<App id: 78, name: "Name">]
User.last.subscriptions
=> [#<Subscription id: 78, app_id: 78>, #<Subscription id: 79, app_id: 78>]
楷模
User
has_many :subscriptions, :dependent => :destroy
has_many :apps, through: :subscriptions
Subscription
validates :user_id, uniqueness: { scope: :app_id }
belongs_to :user, touch: true
belongs_to :app
App
validates_uniqueness_of :name
belongs_to :user, touch: true
belongs_to :app