14

在 Bryan Helmkamp 名为“重构 Fat ActiveRecord 模型的 7 种模式”的优秀博客文章中,他提到了使用Form Objects抽象出多层表单并停止使用accepts_nested_attributes_for.

编辑:请参阅下面的解决方案。

我几乎完全复制了他的代码示例,因为我有同样的问题要解决:

class Signup
  include Virtus

  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations

  attr_reader :user
  attr_reader :account

  attribute :name, String
  attribute :account_name, String
  attribute :email, String

  validates :email, presence: true
  validates :account_name,
    uniqueness: { case_sensitive: false },
    length: 3..40,
    format: { with: /^([a-z0-9\-]+)$/i }

  # Forms are never themselves persisted
  def persisted?
    false
  end

  def save
    if valid?
      persist!
      true
    else
      false
    end
  end

private

  def persist!
    @account = Account.create!(name: account_name)
    @user = @account.users.create!(name: name, email: email)
  end
end

我的代码中的不同之处之一是我需要验证帐户名称(和用户电子邮件)的唯一性。但是,ActiveModel::Validations没有uniqueness验证器,因为它应该是ActiveRecord.

我想有三种方法可以解决这个问题:

  • 写我自己的方法来检查这个(感觉多余)
  • 包括 ActiveRecord::Validations::UniquenessValidator (试过这个,没有让它工作)
  • 或者在数据存储层添加约束

我更喜欢使用最后一个。但后来我一直想知道将如何实现这一点。

我可以做类似的事情(元编程,我需要修改其他一些领域)

  def persist!
    @account = Account.create!(name: account_name)
    @user = @account.users.create!(name: name, email: email)
  rescue ActiveRecord::RecordNotUnique
    errors.add(:name, "not unique" )
    false
  end

但是现在我在课堂上运行了两项检查,首先我使用valid?,然后我使用rescue数据存储约束的语句。

有谁知道处理这个问题的好方法?或许为此编写我自己的验证器会更好(但是我会对数据库进行两个查询,理想情况下一个就足够了)。

4

2 回答 2

12

如果这恰好是一次性要求,那么创建自定义验证器可能会有点过分。

一种简化的方法...

class Signup

  (...)

  validates :email, presence: true
  validates :account_name, length: {within: 3..40}, format: { with: /^([a-z0-9\-]+)$/i }

  # Call a private method to verify uniqueness

  validate :account_name_is_unique


  def persisted?
    false
  end

  def save
    if valid?
      persist!
      true
    else
      false
    end
  end

  private

  # Refactor as needed

  def account_name_is_unique
    if Account.where(name: account_name).exists?
      errors.add(:account_name, 'Account name is taken')
    end
  end

  def persist!
    @account = Account.create!(name: account_name)
    @user = @account.users.create!(name: name, email: email)
  end
end
于 2013-04-23T18:28:35.317 回答
9

Bryan 很友好地在他的博客文章中评论了我的问题。在他的帮助下,我提出了以下自定义验证器:

class UniquenessValidator < ActiveRecord::Validations::UniquenessValidator
  def setup(klass)
    super
    @klass = options[:model] if options[:model]
  end

  def validate_each(record, attribute, value)
    # UniquenessValidator can't be used outside of ActiveRecord instances, here
    # we return the exact same error, unless the 'model' option is given.
    #
    if ! options[:model] && ! record.class.ancestors.include?(ActiveRecord::Base)
      raise ArgumentError, "Unknown validator: 'UniquenessValidator'"

    # If we're inside an ActiveRecord class, and `model` isn't set, use the
    # default behaviour of the validator.
    #
    elsif ! options[:model]
      super

    # Custom validator options. The validator can be called in any class, as
    # long as it includes `ActiveModel::Validations`. You can tell the validator
    # which ActiveRecord based class to check against, using the `model`
    # option. Also, if you are using a different attribute name, you can set the
    # correct one for the ActiveRecord class using the `attribute` option.
    #
    else
      record_org, attribute_org = record, attribute

      attribute = options[:attribute].to_sym if options[:attribute]
      record = options[:model].new(attribute => value)

      super

      if record.errors.any?
        record_org.errors.add(attribute_org, :taken,
          options.except(:case_sensitive, :scope).merge(value: value))
      end
    end
  end
end

您可以在 ActiveModel 类中使用它,如下所示:

  validates :account_name,
    uniqueness: { case_sensitive: false, model: Account, attribute: 'name' }

您遇到的唯一问题是您的自定义model类是否也有验证。当您调用时,这些验证不会运行Signup.new.save,因此您必须以其他方式检查这些验证。您始终可以save(validate: false)在上述方法中使用,但是当您更改或中的任何验证时persist!,您必须确保所有验证都在Signup类中,并保持该类是最新的。AccountUser

于 2013-02-03T11:29:41.687 回答