class User < ActiveRecord::Base
belongs_to :account
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable
# :validatable
validates_presence_of :email
validates :email, format: {with: /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/ }
# validates_uniqueness_of :email
validates_presence_of :password, if: :password_required?
validates_confirmation_of :password, if: :password_required?
validates_length_of :password, within: 6..30, allow_blank: true
мне нужно было снять уникальность email для User и я закомментировал модуль # :validatable добавив из него метод
protected
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
для работы системы.
Но теперь перестала работать авторизация пользователей.
class SessionsController < Devise::SessionsController
before_filter :check_captcha, only: [:create]
skip_before_filter :require_no_authentication, only: [:create,:login_token]
prepend_before_filter :allow_params_authentication!, only: :create
def create
data = {result: :error}
if user_params[:email].present? and user_params[:password].present?
user = User.where(email: user_params[:email].strip.downcase).where.not(role: 'notify')
if user.present? and user.valid_password?(user_params[:password])
sign_in user
data = {result: admin_root_url}
end
end
render json: data
end
NoMethodError: undefined method `valid_password?' for #<User::ActiveRecord_Relation:0x005620fa18ff30>
from /home/user_name/.rvm/gems/ruby-2.1.2@wifibit/gems/activerecord-4.2.4/lib/active_record/relation/delegation.rb:136:in `method_missing'
пробовал в свмой моделе User переопределить метод
def valid_password?(password)
Devise::Encryptor.compare(self.class, encrypted_password, password)
end
и в private и в protected и просто, не видит и все.
Еще для снятия уникальности провел миграцию
class ChangeIndexUsers< ActiveRecord::Migration
def up
remove_index :users, :email
add_index "users", ["email"], name: "index_users_on_email", unique: false, using: :btree
end
def down
remove_index :users, :email
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
end
end