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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:25:05+00:00 2026-06-05T04:25:05+00:00

In rails 3.2.3, I want to validate that a link model has a unique

  • 0

In rails 3.2.3, I want to validate that a link model has a unique combination of two fields. I have a test and a validation that passes the test as shown below, but it seems there may be a better way to do this. For instance, would it be better to use an index with uniqueness? If so, why?

# link_test.rb
...
test "cannot create two links with same name and url" do
  Link.create!(:name => 'example', :url => 'http://www.example.com')
  assert_raise(ActiveRecord::RecordInvalid, 'could create two links with same name and url'){Link.create!(:name => 'example', :url => 'http://www.example.com')}
end
...

# link.rb
class Link < ActiveRecord::Base
  ...
  validates :name, :uniqueness => {:scope => :url, :message => 'cannot have two entries with same name and url'}
  ...
end
  • 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-05T04:25:07+00:00Added an answer on June 5, 2026 at 4:25 am

    I would use both.

    The advantages to using a unique index is that the database will also enforce it. If, for instance, you have a half a dozen application servers each one of which might create a new record then protecting uniqueness in rails is extremely difficult to understate the problem. The disadvantage is that this is harder to recover from.

    The advantages to doing it in in active record is that you can easily debug it, get clean exception messages and communicate the problem to the user more effectively. The disadvantage is that it requires an extra query and it is more vulnerable to multithread/multiprocess type errors.

    With both you get the recoverability of an active record validation when it is possible combined with the absolute reliability of a database unique index. You can still see a different error in the rare instances when two requests hit at the exact same moment, but it will keep your data inline with your constraints.

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

Sidebar

Related Questions

I have a user model that has many fields (let's say 30 for this
I want to validate a date in my model in Ruby on Rails, however,
I have a rails model that validates uniqueness of 2 form values. If these
In a blog in Rails I want to have paths like http://mydomain.com/posts/28383/comments#21 This is
I have the following contrived example in Rails. I want to make sure the
In a Ruby on Rails 3 application, I have invitations. Here is the model:
I have this validation in my user model. validates_uniqueness_of :email, :case_sensitive => false, :message
I have a problem with the scoped uniqueness validation in Rails. It works fine
For my app, I have different signup entry points that validate things differently. So
I'm writing a simple Rails model called Person that has_many :phone_numbers and I'm trying

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.