1
0
Fork 0
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:
Pedro Padron 2012-10-30 15:29:47 -02:00 committed by Carlos Antonio da Silva
parent c3e573db54
commit ba98dad113
3 changed files with 31 additions and 4 deletions

View file

@ -1,5 +1,10 @@
## 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.
*Alexey Vasiliev aka leopard*

View file

@ -71,7 +71,12 @@ module ActiveRecord
end
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?
# will use SQL LOWER function before comparison, unless it detects a case insensitive collation

View file

@ -30,6 +30,11 @@ class ReplyWithTitleObject < Reply
def title; ReplyTitle.new; end
end
class Employee < ActiveRecord::Base
self.table_name = 'postgresql_arrays'
validates_uniqueness_of :nicknames
end
class UniquenessValidationTest < ActiveRecord::TestCase
fixtures :topics, 'warehouse-things', :developers
@ -341,16 +346,28 @@ class UniquenessValidationTest < ActiveRecord::TestCase
assert w6.errors[:city].any?, "Should have errors for city"
assert_equal ["has already been taken"], w6.errors[:city], "Should have uniqueness message for city"
end
def test_validate_uniqueness_with_conditions
Topic.validates_uniqueness_of(:title, :conditions => Topic.where('approved = ?', true))
Topic.create("title" => "I'm a topic", "approved" => true)
Topic.create("title" => "I'm an unapproved topic", "approved" => false)
t3 = Topic.new("title" => "I'm a topic", "approved" => true)
assert !t3.valid?, "t3 shouldn't be valid"
t4 = Topic.new("title" => "I'm an unapproved topic", "approved" => false)
assert t4.valid?, "t4 should be valid"
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