mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added support for validates_uniqueness_of in PostgreSQL array columns. Fixes: #8075.
This commit is contained in:
parent
c3e573db54
commit
ba98dad113
3 changed files with 31 additions and 4 deletions
|
@ -1,5 +1,10 @@
|
||||||
## Rails 4.0.0 (unreleased) ##
|
## Rails 4.0.0 (unreleased) ##
|
||||||
|
|
||||||
|
* Added support for `validates_uniqueness_of` in PostgreSQL array columns.
|
||||||
|
Fixes #8075.
|
||||||
|
|
||||||
|
*Pedro Padron*
|
||||||
|
|
||||||
* Allow int4range and int8range columns to be created in PostgreSQL and properly convert to/from database.
|
* Allow int4range and int8range columns to be created in PostgreSQL and properly convert to/from database.
|
||||||
|
|
||||||
*Alexey Vasiliev aka leopard*
|
*Alexey Vasiliev aka leopard*
|
||||||
|
|
|
@ -71,7 +71,12 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
column = klass.columns_hash[attribute.to_s]
|
column = klass.columns_hash[attribute.to_s]
|
||||||
value = column.limit ? value.to_s[0, column.limit] : value.to_s if !value.nil? && column.text?
|
|
||||||
|
if !value.nil? && column.text? && column.limit
|
||||||
|
value = value.to_s[0, column.limit]
|
||||||
|
else
|
||||||
|
value = klass.connection.type_cast(value, column)
|
||||||
|
end
|
||||||
|
|
||||||
if !options[:case_sensitive] && value && column.text?
|
if !options[:case_sensitive] && value && column.text?
|
||||||
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation
|
||||||
|
|
|
@ -30,6 +30,11 @@ class ReplyWithTitleObject < Reply
|
||||||
def title; ReplyTitle.new; end
|
def title; ReplyTitle.new; end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Employee < ActiveRecord::Base
|
||||||
|
self.table_name = 'postgresql_arrays'
|
||||||
|
validates_uniqueness_of :nicknames
|
||||||
|
end
|
||||||
|
|
||||||
class UniquenessValidationTest < ActiveRecord::TestCase
|
class UniquenessValidationTest < ActiveRecord::TestCase
|
||||||
fixtures :topics, 'warehouse-things', :developers
|
fixtures :topics, 'warehouse-things', :developers
|
||||||
|
|
||||||
|
@ -353,4 +358,16 @@ class UniquenessValidationTest < ActiveRecord::TestCase
|
||||||
t4 = Topic.new("title" => "I'm an unapproved topic", "approved" => false)
|
t4 = Topic.new("title" => "I'm an unapproved topic", "approved" => false)
|
||||||
assert t4.valid?, "t4 should be valid"
|
assert t4.valid?, "t4 should be valid"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_validate_uniqueness_with_array_column
|
||||||
|
return skip "Uniqueness on arrays has only been tested in PostgreSQL so far." if !current_adapter? :PostgreSQLAdapter
|
||||||
|
|
||||||
|
e1 = Employee.create("nicknames" => ["john", "johnny"], "commission_by_quarter" => [1000, 1200])
|
||||||
|
assert e1.persisted?, "Saving e1"
|
||||||
|
|
||||||
|
e2 = Employee.create("nicknames" => ["john", "johnny"], "commission_by_quarter" => [2200])
|
||||||
|
assert !e2.persisted?, "e2 shouldn't be valid"
|
||||||
|
assert e2.errors[:nicknames].any?, "Should have errors for nicknames"
|
||||||
|
assert_equal ["has already been taken"], e2.errors[:nicknames], "Should have uniqueness message for nicknames"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue