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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:17:32+00:00 2026-05-14T03:17:32+00:00

In Rails 2.x you can use validations to make sure you have a unique

  • 0

In Rails 2.x you can use validations to make sure you have a unique combined value like this:

validates_uniqueness_of :husband, :scope => :wife

In the corresponding migration it could look like this:

add_index :family, [:husband, :wife], :unique => true

This would make sure the husband/wife combination is unique in the database. Now, in Rails 3 the validation syntax changed and the scope attribute seems to be gone. It now looks like:

validates :husband, :presence => true

Any idea how I can achieve the combined validation in Rails 3? The Rails 2.x validations still work in Rails 3 so I can still use the first example but it looks so “old”, are there better ways?

  • 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-14T03:17:33+00:00Added an answer on May 14, 2026 at 3:17 am

    Bear with me. The way the validates method in ActiveModel works is to look for a Validator.

    :presence => true looks for PresenceValidator and passes the options: true to the validator’s initializer.

    I think you want

    validates :husband, :presence => true, :uniqueness => {:scope => :wife}
    

    (The uniqueness validator is actually part of ActiveRecord, not ActiveModel. It’s really interesting how the developers set this up. It’s quite elegant.)

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

Sidebar

Related Questions

Does anyone know how i can use BST for config.time_zone in my rails config/environment.rb
Can I use MongoDB and a PostgreSQL in one rails app? Specifically I will
Can I set rails to use mysql with MEMORY as the DB engine? I
My Rails application makes use of Time.zone so that each user can set their
I'm trying to get mysql gem installed for use on rails, using jruby, can't
A single Rails command can make lots of changes in an app - creating
In ruby on rails 3 you can use their ActiveMailer for sending emails. Also
I am using koala(1.3.0) with rails (3.0.7). This is how I use them. Link
I have a SharePoint server. I am building an rails application which can interact
In rails 3.1 you can use the awesome rake task to copy in migrations

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.