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 6102809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:39:26+00:00 2026-05-23T13:39:26+00:00

I created a callback when creating a new Client object instead of using the

  • 0

I created a callback when creating a new Client object instead of using the validates_uniqueness_of validation method because:

  • I want to find the Client that already exists
  • Add the existing Client details to my base error message

My question: is there a more elegant way of achieving this than my solution below?

I have the following model:

class Client < ActiveRecord::Base
  before_validation_on_create :prevent_duplicate_clients

  private
    def prevent_duplicate_clients
      client = self.class.find(:all, :conditions => ["first_name = ? AND middle_name = ? AND last_name = ? AND date_of_birth = ?", self.first_name, self.middle_name, self.last_name, self.date_of_birth])

      if client.size >= 1
        self.errors.add(:base, "Client exists as #{client.first.name}")
        false
      end
    end
end

NB:

  • Rails v2.3.5
  • 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-05-23T13:39:26+00:00Added an answer on May 23, 2026 at 1:39 pm

    You shouldn’t put validation code in the before_validation callbacks. Change before_validation_on_create :prevent_duplicate_clients to validate :prevent_duplicate_clients

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

Sidebar

Related Questions

I've created this basic 3D Demo using OpenGL/SDL. I handled the keyboard callback so
I'm creating a site using drupal where I'd like to prompt my new users
Where can I find the method signature for callback methods in built-in classes like
I'm creating a JavaScript class (using Prototype) that will set the page state to
I'm creating a barcode .png in a callback using the Barby gem : Isbn.rb:
I have some javascript code that creates an img tag with a mouseover callback,
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I created a few mediawiki custom tags, using the guide found here http://www.mediawiki.org/wiki/Manual:Tag_extensions I
I'm working on refactoring a suite of old utilities and creating a new server
In my application, I want to create entries in certain tables when a new

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.