Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Desive向用户模型添加作用域_Ruby On Rails_Devise_Multi Tenant - Fatal编程技术网

Ruby on rails 使用Desive向用户模型添加作用域

Ruby on rails 使用Desive向用户模型添加作用域,ruby-on-rails,devise,multi-tenant,Ruby On Rails,Devise,Multi Tenant,我正在尝试使用子域ala railscast 388构建一个多租户应用程序。我使用Desive进行身份验证,因此它添加了一个不同的扭曲,导致了一个问题。当我尝试添加一个新用户时,遇到以下错误,该用户的电子邮件已在另一个子域帐户下生成: ActiveRecord::StatementInvalid in RegistrationsController#create SQLite3::ConstraintException: constraint failed: INSERT INTO "user

我正在尝试使用子域ala railscast 388构建一个多租户应用程序。我使用Desive进行身份验证,因此它添加了一个不同的扭曲,导致了一个问题。当我尝试添加一个新用户时,遇到以下错误,该用户的电子邮件已在另一个子域帐户下生成:

ActiveRecord::StatementInvalid in RegistrationsController#create

SQLite3::ConstraintException: constraint failed: INSERT INTO "users" ("account_id", "created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
如果新用户的电子邮件在数据库中的任何位置都不存在,那么添加新用户的效果会非常好,因此我假设在如何将范围添加到电子邮件验证中存在问题。我禁用了:Validable Desive模块,并将验证添加到我的用户模型中:

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable


  attr_accessible :email, :password, :password_confirmation, :remember_me
  belongs_to :account


  validates_uniqueness_of :email, :scope => :account_id, :case_sensitive => false, :allow_blank => true, :if => :email_changed?
  validates_format_of :email, :with => Devise.email_regexp, :allow_blank => true, :if => :email_changed?
  validates_presence_of :password, :on=>:create
  validates_confirmation_of :password, :on=>:create
  validates_length_of :password, :within => Devise.password_length, :allow_blank => true

  default_scope { where(account_id: Account.current_id) }

end

class Account < ActiveRecord::Base
  attr_accessible :name, :subdomain
  has_many :clients, :dependent => :destroy
  has_many :users
  cattr_accessor :current_id

end

class ApplicationController < ActionController::Base
  protect_from_forgery

  around_filter :scope_current_account

private

  def current_account
    Account.find_by_subdomain! request.subdomain
  end
  helper_method :current_account

  def scope_current_account
    Account.current_id = current_account.id
    yield
  ensure
    Account.current_id = nil
  end

end

谢谢

您可能需要创建一个迁移,以消除Desive添加的索引\u users\u on\u email索引上的唯一约束。也许是这样:

def up
  remove_index :users, :name => 'index_users_on_email'
  add_index :users, :email, :unique => false
end

def down
  remove_index :users, :name => 'index_users_on_email'
  add_index :users, :email, :unique => true
end

或者最好添加以下索引:add_index:users,[:account_id,:email],:unique=>true