title: on an ActiveModel Object, how do I check uniqueness?
body: <p>In Bryan Helmkamp's excellent blog post called "<a href="http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/" rel="noreferrer">7 Patterns to Refactor Fat ActiveRecord Models</a>", he mentions using <code>Form Objects</code> to abstract away multi-layer forms and stop using <code>accepts_nested_attributes_for</code>.</p>
  
  <p><strong>Edit: see <a href="https://stackoverflow.com/questions/14670396/on-an-activemodel-object-how-do-i-check-uniqueness#14671979">below</a> for a solution.</strong></p>
  
  <p>I've almost exactly duplicated his code sample, as I had the same problem to solve:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>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
  </code></pre>
  
  <p>One of the things different in my piece of code, is that I need to validate the <strong>uniqueness</strong> of the account name (and user e-mail). However, <code>ActiveModel::Validations</code> doesn't have a <code>uniqueness</code> validator, as it's supposed to be a non-database backed variant of <code>ActiveRecord</code>.</p>
  
  <p>I figured there are three ways to handle this:</p>
  
  <ul>
  <li>Write my own method to check this (feels redundant)</li>
  <li>Include ActiveRecord::Validations::UniquenessValidator (tried this, didn't get it to work)</li>
  <li>Or add the constraint in the data storage layer</li>
  </ul>
  
  <p>I would prefer to use the last one. But then I'm kept wondering <em>how</em> I would implement this.</p>
  
  <p>I could do something like <em>(metaprogramming, I would need to modify some other areas)</em>:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>  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
  </code></pre>
  
  <p>But now I have two checks running in my class, first I use <code>valid?</code> and then I use a <code>rescue</code> statement for the data storage constraints.</p>
  
  <p>Does anyone know of a good way to handle this issue? Would it be better to perhaps write my own validator for this (but then I'd have two queries to the database, where ideally one would be enough).</p>