mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix validation on uniqueness of empty association
This commit is contained in:
parent
34c08d2ead
commit
c449a74e89
3 changed files with 24 additions and 1 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Fix validation on uniqueness of empty association.
|
||||||
|
|
||||||
|
*Evgeny Li*
|
||||||
|
|
||||||
* Make `ActiveRecord::Relation#unscope` affect relations it is merged in to.
|
* Make `ActiveRecord::Relation#unscope` affect relations it is merged in to.
|
||||||
|
|
||||||
*Jon Leighton*
|
*Jon Leighton*
|
||||||
|
|
|
@ -48,7 +48,7 @@ module ActiveRecord
|
||||||
def build_relation(klass, table, attribute, value) #:nodoc:
|
def build_relation(klass, table, attribute, value) #:nodoc:
|
||||||
if reflection = klass.reflect_on_association(attribute)
|
if reflection = klass.reflect_on_association(attribute)
|
||||||
attribute = reflection.foreign_key
|
attribute = reflection.foreign_key
|
||||||
value = value.attributes[reflection.primary_key_column.name]
|
value = value.attributes[reflection.primary_key_column.name] unless value.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
column = klass.columns_hash[attribute.to_s]
|
column = klass.columns_hash[attribute.to_s]
|
||||||
|
|
|
@ -35,6 +35,11 @@ class Employee < ActiveRecord::Base
|
||||||
validates_uniqueness_of :nicknames
|
validates_uniqueness_of :nicknames
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TopicWithUniqEvent < Topic
|
||||||
|
belongs_to :event, foreign_key: :parent_id
|
||||||
|
validates :event, uniqueness: true
|
||||||
|
end
|
||||||
|
|
||||||
class UniquenessValidationTest < ActiveRecord::TestCase
|
class UniquenessValidationTest < ActiveRecord::TestCase
|
||||||
fixtures :topics, 'warehouse-things', :developers
|
fixtures :topics, 'warehouse-things', :developers
|
||||||
|
|
||||||
|
@ -376,4 +381,18 @@ class UniquenessValidationTest < ActiveRecord::TestCase
|
||||||
assert_equal ["has already been taken"], e2.errors[:nicknames], "Should have uniqueness message for nicknames"
|
assert_equal ["has already been taken"], e2.errors[:nicknames], "Should have uniqueness message for nicknames"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_validate_uniqueness_on_existing_relation
|
||||||
|
event = Event.create
|
||||||
|
assert TopicWithUniqEvent.create(event: event).valid?
|
||||||
|
|
||||||
|
topic = TopicWithUniqEvent.new(event: event)
|
||||||
|
assert_not topic.valid?
|
||||||
|
assert_equal ['has already been taken'], topic.errors[:event]
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_validate_uniqueness_on_empty_relation
|
||||||
|
topic = TopicWithUniqEvent.new
|
||||||
|
assert topic.valid?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue