Skip to content

Commit

Permalink
Merge pull request rails#7072 from beerlington/fix_validations_with_s…
Browse files Browse the repository at this point in the history
…cope

Use database value for uniqueness validation scope
  • Loading branch information
jonleighton committed Jul 17, 2012
2 parents 15bfaa3 + 0c3c227 commit d0ba994
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/validations/uniqueness.rb
Expand Up @@ -26,7 +26,7 @@ def validate_each(record, attribute, value)
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?

Array(options[:scope]).each do |scope_item|
scope_value = record.send(scope_item)
scope_value = record.read_attribute(scope_item)
reflection = record.class.reflect_on_association(scope_item)
if reflection
scope_value = record.send(reflection.foreign_key)
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/validations/uniqueness_validation_test.rb
Expand Up @@ -22,6 +22,14 @@ class Conjurer < IneptWizard
class Thaumaturgist < IneptWizard
end

class ReplyTitle; end

class ReplyWithTitleObject < Reply
validates_uniqueness_of :content, :scope => :title

def title; ReplyTitle.new; end
end

class UniquenessValidationTest < ActiveRecord::TestCase
fixtures :topics, 'warehouse-things', :developers

Expand Down Expand Up @@ -104,6 +112,14 @@ def test_validate_uniqueness_with_object_scope
assert !r2.valid?, "Saving r2 first time"
end

def test_validate_uniqueness_with_composed_attribute_scope
r1 = ReplyWithTitleObject.create "title" => "r1", "content" => "hello world"
assert r1.valid?, "Saving r1"

r2 = ReplyWithTitleObject.create "title" => "r1", "content" => "hello world"
assert !r2.valid?, "Saving r2 first time"
end

def test_validate_uniqueness_with_object_arg
Reply.validates_uniqueness_of(:topic)

Expand Down

0 comments on commit d0ba994

Please sign in to comment.