[英]How prevent a user log in twice with the same username - using Rails Authlogic?
[英]How to prevent user to subscribe twice to the same App?
我有以下型號。
我不希望該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
您可以向關聯添加唯一約束:
has_many :apps, through: :subscriptions, uniq: true
我向訂閱表中的數據庫添加了唯一索引。
add_index :subscriptions, [:user_id, :app_id], unique: true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.