Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9238941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:52:32+00:00 2026-06-18T07:52:32+00:00

In Bryan Helmkamp’s excellent blog post called 7 Patterns to Refactor Fat ActiveRecord Models

  • 0

In Bryan Helmkamp’s excellent blog post called “7 Patterns to Refactor Fat ActiveRecord Models“, he mentions using Form Objects to abstract away multi-layer forms and stop using accepts_nested_attributes_for.

Edit: see below for a solution.

I’ve almost exactly duplicated his code sample, as I had the same problem to solve:

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

One of the things different in my piece of code, is that I need to validate the uniqueness of the account name (and user e-mail). However, ActiveModel::Validations doesn’t have a uniqueness validator, as it’s supposed to be a non-database backed variant of ActiveRecord.

I figured there are three ways to handle this:

  • Write my own method to check this (feels redundant)
  • Include ActiveRecord::Validations::UniquenessValidator (tried this, didn’t get it to work)
  • Or add the constraint in the data storage layer

I would prefer to use the last one. But then I’m kept wondering how I would implement this.

I could do something like (metaprogramming, I would need to modify some other areas):

  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

But now I have two checks running in my class, first I use valid? and then I use a rescue statement for the data storage constraints.

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:52:33+00:00Added an answer on June 18, 2026 at 7:52 am

    Bryan was kind enough to comment on my question to his blog post. With his help, I’ve come up with the following custom validator:

    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
    

    You can use it in your ActiveModel classes like so:

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

    The only problem you’ll have with this, is if your custom model class has validations as well. Those validations aren’t run when you call Signup.new.save, so you will have to check those some other way. You can always use save(validate: false) inside the above persist! method, but then you have to make sure all validations are in the Signup class, and keep that class up to date, when you change any validations in Account or User.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry, I have another problem now. I'm using a new php form method than
I recently posted a question which was answered by Bryan Watts, regarding generic repository
I was reading some of the concurrency patterns in Brian Goetze's Java Concurrency in
I'm trying to use my fixtures in a UnitTest. AddFavoritesTestCase(unittest.TestCase): fixtures = ['/Users/Bryan/work/osqa/fixtures/fixture_questions.json'] def
I'm using this script to run a command within each subdirectory in the 'sites'
I'm creating a 2D MMORPG game (in an applet form) and I had a
there is a file called list.lst which I wrote down its content. I need
I'm trying to get the not confirmed warning to go away when using Yahoo!
I'm trying to add images to my models in my Django app. models.py class
I've set up gitosis using the tutorial privided at: http://scie.nti.st/2007/11/14/hosting-git-repositories-the-easy-and-secure-way I found that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.