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

unique with scope broken #17

Closed
djberg96 opened this issue Dec 1, 2010 · 2 comments
Closed

unique with scope broken #17

djberg96 opened this issue Dec 1, 2010 · 2 comments

Comments

@djberg96
Copy link

djberg96 commented Dec 1, 2010

Rails 3.0.3
CPK 3.0.7
PostgreSQL 8.x

The following code that uses a unique + scope validation (to validate uniqueness for a two column combo) is broken with CPK. Instead of getting "has already been taken", I'm getting "translation missing: en, activerecord, errors, models, location, attributes, siteid, taken".


  require 'composite_primary_keys'
  require 'active_record'
  class Location < ActiveRecord::Base
    validates :siteid,
      :length     => {:is => 6},
      :uniqueness => {:scope => :schoolid},
      :presence   => true
  end

  location = Location.new
  location.schoolid = 'SOMEXX'
  location.siteid = 'ATM-01'
  p location.valid?
  p location.errors
@djberg96
Copy link
Author

djberg96 commented Dec 1, 2010

This patch fixes it. Copied from the current AR code:

>diff -u uniqueness.orig uniqueness.rb
--- uniqueness.orig 2010-12-01 10:22:15.172986009 -0700
+++ uniqueness.rb   2010-12-01 10:24:00.768986805 -0700
@@ -31,7 +31,7 @@
         end
 
         if relation.exists?
-          record.errors.add(attribute, :taken, :default => options[:message], :value => value)
+          record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
         end
       end
     end

@cfis
Copy link
Contributor

cfis commented Dec 4, 2010

Thanks Daniel. Change committed (well, I resynced with the new active record version of this method and reapplied the cpk changes).

This issue was closed.
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