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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:45:27+00:00 2026-05-15T14:45:27+00:00

I have a basic Rails model with two properties, name and code . I

  • 0

I have a basic Rails model with two properties, name and code. I have validates_uniqueness_of for the code property. However, I’d like to customize the :message to show the name of the duplicate. Is there any way to access this duplicate item?

For example, say I first enter a record called Expired with code EXP. Then I enter Experience with code EXP. I would like the :message to say something like “Code already taken by Expired”.

> m1 = Model.new(:name => 'Expired', :code => 'EXP')
> m2 = Model.new(:name => 'Experience', :code => 'EXP')
> m1.save!
> m2.save!

validates_uniqueness_of :code,
  :message => "Code already taken by #{duplicate.name}"

Is there any built-in Rails construct that holds the duplicate object so that I can access it like I do in the :message? Or is there any other way I can run code to determine the duplicate when this validation gets triggered?

  • 1 1 Answer
  • 1 View
  • 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-15T14:45:28+00:00Added an answer on May 15, 2026 at 2:45 pm

    I believe you’d have to write a custom validation method. Something like:

    def validate
       model = Model.first(:conditions => {:code => self.code})
       unless model.blank? && (self.id != model.id)
         errors.add_to_base "Code already taken by #{model.name}"
       end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model setup in my Ruby on Rails app. (User {name,
I have a pretty basic 'User' model, control & views for a Rails app.
I'm trying to get the hang of basic Rails routing. I have a model
I have a basic authentication system just like in Michael Hartl's Ruby on Rails
I have a Rails 3.2.2 application using Ruby version 1.9.2. I have created basic
I have basic Spring MVC 3 setup for i18n where I can show labels
I have some basic jquery code sitting in the header of the header file
I have a basic.xsd and two other A.xsd and B.xsd . A.xsd and B.xsd
I have a basic problem with a has_one and belongs_to association. I have two
I have a Rails 2.3.x app that implements the act_as_authentic in User model and

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.