【问题标题】:undefined method `exists?' for ActiveRecord::Associations::Builder:Module未定义的方法“存在吗?”对于 ActiveRecord::Associations::Builder:Module
【发布时间】:2013-09-23 07:20:43
【问题描述】:

我有带有电子邮件字段的 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 在builder 模型,但是如果我签入 User 模型,为什么会抛出错误,尽管代码是正确的。

class User < ActiveRecord::Builder

发生错误: 退出 /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:inblock in constantize'

【问题讨论】:

  • Builder 是您数据库中的模型吗?
  • 是的,我有两个从 Devise 创建的模型 User 和 Builder。如果 Builder 中存在电子邮件,则不应将其保存在 User 中,反之亦然。
  • 您能否澄清这一点:“当我放入 Builder 模型而不是 User 模型时,以下验证方法工作正常。”我不太明白意思。

标签: ruby-on-rails ruby-on-rails-3.2 devise ruby-1.9.3


【解决方案1】:

从错误看来,Builder 指的是在 ActiveRecord 范围内定义的ActiveRecord::Associations::Builder 模块。

尝试使用::Builder 访问您的模型,所以:

  if ::Builder.exists?(email: email)

【讨论】:

  • 非常感谢 AJcodez,这真的节省了我的时间。现在可以正常使用了。
【解决方案2】:

为什么不使用默认验证来保证唯一性

class User < ActiveRecord::Base
  ...
  validates :email, :uniqueness => true, :message => "User already exists with this email, try another email"
  ...
end

同样在上面提到的代码中,你应该使用 User 模型而不是 Builder

class User < ActiveRecord::Base
  ...
  def check_email_exists
    if User.exists?(:email => self.email)
      errors.add(:email,"User already exists with this email, try another email")
    end
  end 
  ...
end

【讨论】:

  • 阿曼,在使用已包含类似功能的 Devise 时,您甚至不需要此验证。 OP 的问题是关于跨模型的。
  • @Aman Garg:默认情况下,电子邮件在设计模型中是唯一的。比利陈说的是真的。
猜你喜欢
  • 2016-11-18
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多