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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:53:07+00:00 2026-06-11T16:53:07+00:00

Table ‘leave_policies’ has year, increment and total_entitled fields. I have written ‘ validates_uniqueness_of :year

  • 0

Table ‘leave_policies’ has year, increment and total_entitled fields.

I have written ‘validates_uniqueness_of :year‘ in LeavePolicy model.

update_attributes not working even I am not updating year field.

Please guide for mistake or suggest any better solution.

Class LeavePolicy
  validates_uniqueness_of :year
end

@leave_policy is object of LeavePolicy

In table a row with :id = 1 ,year = 1 , increment= 2 , total_entitled = 3. If I update row with id : 1 like

@leave_policy.update_attributes(:total_entitled => 5)

I got the error “year is already taken”.

  • 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-11T16:53:08+00:00Added an answer on June 11, 2026 at 4:53 pm

    You are sure there is no other row with year of 1, and that the @leave_policy is actually an instance of the row with id 1?

    If it were actually a new record, it would fail this way, or if there were any other record with :year == 1.

    Validations are run on all attributes, even if you use update_attributes only on some, so if the year is not unique, it will fail even if you are only updating the increment or total_entitled fields.

    In general, validates_uniqueness is not a great answer on its own, as race conditions from checking in ruby can prevent it from enforcing uniqueness, so I would also use it with a unique key in the database.

    If you want to be able to skip the unique year validation when changing the other fields, you can set the attributes on the model, then call

    leave_policy.save(:validate => false) #for rails 3
    leave_policy.save(false) #for rails 2
    

    and that will skip all validations, but man, I would think twice.
    That seems to defeat the purpose, so I would instead pursue why it is that rails thinks you do not have a unique year – perhaps a look in your db will show there really is a dupe row for the year == 1.

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

Sidebar

Related Questions

Table A has a number of rows, only some of which have (multiple) children
Table has only 2 fields: VehicleID and FacilitiesID . FacilitiesID is the checkbox list
Table Category (c) has a 1:many relationship with Table Question: a Category can have
Table A has two columns: ID and name I want to input 3 ID
Table #1: qa_returns_items Table #2: qa_returns_residues I have a long time trying to get
Table name: Employee Column Name: Emp_name Emp_name has this contents: xx\rama, xx\rajesh, xx\vignesh I
Table data has border and a link image and some other anchor text below
Table: Relatives emp_id dep_id (composite primary key) We have to restrict one employee to
Table User contains the fields id, name, fruit, veggies, meat. Table FruitType contains the
Table 1: users Fields: id, email, first_name Table 2: resources Fields: user_id, product_id I

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.