[英]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.