我有带有电子邮件字段的 Builder 和 User 模型,我想让电子邮件在这两个模型中都是唯一的。当我放入 Builder 模型而不是 User 模型时,以下验证方法工作正常。
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable, :confirmable
attr_accessible :email, :password, :password_confirmation, :remember_me,:confirmation_token, :confirmed_at, :confirmation_sent_at, :unconfirmed_email, :provider,:uid, :name, :oauth_token, :oauth_expires_at
validate :check_email_exists
def check_email_exists
if Builder.exists?(:email => self.email)
errors.add(:email,"User already exists with this email, try another email")
end
end
错误是:
NoMethodError in Devise::RegistrationsController#create
app/models/user.rb:30:in `check_email_exists'
{"utf8"=>"✓",
"authenticity_token"=>"EiFhJta51puZ7HZA3YzhopsKL2aJWllkl8geo3cL3gc=",
"user"=>{"email"=>"builder@gmail.com",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}
错误的原因是什么?我试图从很多天解决它,但没有成功。
这是我的建造者模型
class Builder < ActiveRecord::Base
devise :database_authenticatable, :registerable,
attr_accessible :email, :password, :password_confirmation, :remember_me,
validate :email_exists
def email_exists
if User.exists?(:email => self.email)
errors.add(:email,"User already exists with this email, try another email")
end
end
让 abc@gmail.com 在用户中已经存在,生成器注册表单将告诉用户已经存在 如果我在生成器注册表单中使用 abc@gmail.com 注册,请尝试另一封电子邮件,这意味着 email_exists 在生成器模型中工作正常但是如果我签入用户模型,为什么会抛出错误,尽管代码是正确的。
class User < ActiveRecord::Builder
发生错误:Exiting /home/rails/Desktop/realestate/app/models/user.rb:1:in <top (required)>': uninitialized constant ActiveRecord::Builder (NameError)
from /home/rails/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.13/lib/active_support/inflector/methods.rb:230:in
block in constantize'