{"action":"created","issue":{"url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/830","labels_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/830/labels{/name}","comments_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/830/comments","events_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/830/events","html_url":"https://github.com/thoughtbot/shoulda-matchers/issues/830","id":114305684,"number":830,"title":"validate_uniqueness_of validation fails when having more than one validates to the same field","user":{"login":"luismaia","id":4895808,"avatar_url":"https://avatars.githubusercontent.com/u/4895808?v=3","gravatar_id":"","url":"https://api.github.com/users/luismaia","html_url":"https://github.com/luismaia","followers_url":"https://api.github.com/users/luismaia/followers","following_url":"https://api.github.com/users/luismaia/following{/other_user}","gists_url":"https://api.github.com/users/luismaia/gists{/gist_id}","starred_url":"https://api.github.com/users/luismaia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/luismaia/subscriptions","organizations_url":"https://api.github.com/users/luismaia/orgs","repos_url":"https://api.github.com/users/luismaia/repos","events_url":"https://api.github.com/users/luismaia/events{/privacy}","received_events_url":"https://api.github.com/users/luismaia/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-30T16:29:54Z","updated_at":"2015-10-30T16:44:02Z","closed_at":null,"body":"I have the following validations in one of my models:\r\n\r\n```\r\nclass CalibrationConstantVersion < ActiveRecord::Base\r\n  (...)\r\n  validates :calibration_constant,\r\n            presence: true,\r\n            uniqueness: { scope: [:name], case_sensitive: false }\r\n\r\n  validates :calibration_constant,\r\n            presence: true,\r\n            uniqueness: { scope: [:begin_at, :physical_device_id] }\r\n  (...)\r\nend\r\n```\r\n\r\nAnd my RSpec is:\r\n\r\n```\r\nRSpec.describe CalibrationConstantVersion do\r\n  (...)\r\n  it { should validate_uniqueness_of(:calibration_constant).scoped_to(:name).case_insensitive }\r\n  it { should validate_uniqueness_of(:calibration_constant).scoped_to(:begin_at, :physical_device_id)}\r\n  (...)\r\nend\r\n```\r\n\r\nWhen upgrading from version 2.8.0 to 3.0.1 I start having this error:\r\n\r\n```\r\nFailures:\r\n\r\n  1) CalibrationConstantVersion should require unique value for calibration_constant scoped to begin_at, physical_device_id\r\n     Failure/Error: it { expect(create(:calibration_constant_version)).to validate_uniqueness_of(:calibration_constant).scoped_to(:begin_at, :physical_device_id)}\r\n       Expected validation to be scoped to [:begin_at, :physical_device_id], but it was scoped to [:name].\r\n```\r\n\r\nIf I exchange the validates order, in the model I got this error:\r\n\r\n```\r\nFailures:\r\n\r\n  1) CalibrationConstantVersion should require unique value for calibration_constant scoped to name\r\n     Failure/Error: it { should validate_uniqueness_of(:calibration_constant).scoped_to(:name).case_insensitive }\r\n       Expected validation to be scoped to [:name], but it was scoped to [:begin_at, :physical_device_id].\r\n```\r\n\r\nMy conclusion is that shoulda-matchers (in version 3) is only executing the first validates definition per field. Is this conclusion correct?\r\n\r\nI tested against master and I get the same error.\r\nIs this an issue or am I doing something wrong?\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/comments/152582616","html_url":"https://github.com/thoughtbot/shoulda-matchers/issues/830#issuecomment-152582616","issue_url":"https://api.github.com/repos/thoughtbot/shoulda-matchers/issues/830","id":152582616,"user":{"login":"mcmire","id":7371,"avatar_url":"https://avatars.githubusercontent.com/u/7371?v=3","gravatar_id":"","url":"https://api.github.com/users/mcmire","html_url":"https://github.com/mcmire","followers_url":"https://api.github.com/users/mcmire/followers","following_url":"https://api.github.com/users/mcmire/following{/other_user}","gists_url":"https://api.github.com/users/mcmire/gists{/gist_id}","starred_url":"https://api.github.com/users/mcmire/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mcmire/subscriptions","organizations_url":"https://api.github.com/users/mcmire/orgs","repos_url":"https://api.github.com/users/mcmire/repos","events_url":"https://api.github.com/users/mcmire/events{/privacy}","received_events_url":"https://api.github.com/users/mcmire/received_events","type":"User","site_admin":false},"created_at":"2015-10-30T16:44:02Z","updated_at":"2015-10-30T16:44:02Z","body":"Yes, starting in version 3, the matcher will look at the existing uniqueness validation that is on the attribute in question and will compare the scopes defined there with the scopes you are passing to the \r\nmatcher. To be specific, it looks for the *first* existing uniqueness validation on the attribute. So this obviously assumes that there is only one.\r\n\r\nSo you've definitely found a bug :beetle: "}}