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

  • Home
  • SEARCH
  • 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 8822711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:10:21+00:00 2026-06-14T06:10:21+00:00

I would like to show some extra info in error messages resulting from a

  • 0

I would like to show some extra info in error messages resulting from a failed validation. For example suppose I have a class Book with the following validation

validates :name, presence: true, uniqueness: true

When someone tries to insert a book by the same name the following error message is returned

{"name":["has already been taken"]}

Instead I wanna show

{"name":["Book 'Great Expectaions' has already been taken at id:7"]}

Right now to make this happen I have to remove the uniqueness validation that I mentioned above and do the following

validate do |book|
  existing_book = Book.find_by_name(book.name)
  if existing_book
    book.errors.add(:name, "#{existing_book.name} already exists at id: #{existing_book.id}")
  end
end

Is there a way to get custom error messages like above without writing a custom uniqueness validation? I was thinking something along the lines of

validates :name, presence: true, uniqueness: {message: "#{self.name} already exists at id: #{Book.find_by_name(self.name).id}"

But this does not seem to work as self.name returns ‘Book’. Is there a way to access the passed parameters in this context?

  • 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-14T06:10:22+00:00Added an answer on June 14, 2026 at 6:10 am

    You’ll have to do this as a custom validation. I would do it like so:

    validate :name_is_unique
    
    private
    def name_is_unique
      errors.add(:name, "#{other_book.name} already exists at id: #{other_book.id}") if other_book = Book.find_by_name(name)
    end
    

    The issue isn’t really that you can’t include the current model attributes in your validation, its that there’s no ‘one-liner’ way to include another model. The good news is, that’s what the validate method is for.

    If it bothers you to have this in your model, just write a custom validator so it can be re-used application-wide.

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

Sidebar

Related Questions

I have some text in a UITextView, that I would like to have show
I would like to show loadingActivity while activity1 is executing some code (working), after
I would like to show the value of a variable in a TextView, like
I would like to show and hide a div during hover and hover out.
I would like to show a div based on the Onclick event of an
I would like to show divs at a specific interval (10 seconds) and show
I would like to show different tabs in the top navigation of a SharePoint
I would like to Show my Messagebox in the center of its parent form.
I would like to show a modal progress wheel overlay on my view. The
i would like to show a TimeSpan in a MessageBox but am getting an

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.