Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validates_uniquness_of with scoped_to sporadically fails when scope attribute is a uuid #626

Closed
tyre opened this issue Dec 15, 2014 · 1 comment

Comments

@tyre
Copy link

tyre commented Dec 15, 2014

Let's say I have a pages table with an integer column page_number and a foreign key book_id. I'd like to store pages for many books, but ensure that there is only one of each page number per book.

class Page < ActiveRecord
  validates :page_number, presence: true, uniqueness: { scope: :book_id }
end

In my tests:

it { validates_uniqueness_of(:page_number).scoped_to(:book_id) }

If I am using Postgres and my ids are uuid columns, this will fail roughly once every 16 times.

Specifically, this line:

https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb#L385

When next_value is calculated, the previous_value is a string (a uuid) such as "063d663a-8c2f-4ff1-8eb8-e3e3bcd2a75f". The if and first elsif fail, but the second elsif succeeds. The result of calling #next on that string is "063d663a-8c2f-4ff1-8eb8-e3e3bcd2a75g", which is an invalid uuid (since that trailing g is not hex.)

What do you think the best way to fix this would be? My opinion is that this is trying to be too clever with this whole next bit, rather than calling the generator for that attribute again.

@mcmire
Copy link
Collaborator

mcmire commented Dec 16, 2014

Yup, and this issue has been raised before: #402 and #587.

Sorry this issue has been so long-standing, I'm about to finish up a release, but I will try to make this a priority for the next release. In the meantime you can find a fix in #587.

@mcmire mcmire closed this as completed Dec 16, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants