diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 71491db213..eb0db9d944 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that validates_size_of :within works in associations #11295, #10019 [cavalle] + * Track changes to unsaved attributes. [Jeremy Kemper] * Switched to UTC-timebased version numbers for migrations and the schema. This will as good as eliminate the problem of multiple migrations getting the same version assigned in different branches. Also added rake db:migrate:up/down to apply individual migrations that may need to be run when you merge branches #11458 [jbarnette] diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 7775cf93c5..c321464335 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -553,9 +553,10 @@ module ActiveRecord too_long = options[:too_long] % option_value.end validates_each(attrs, options) do |record, attr, value| - if value.nil? or value.split(//).size < option_value.begin + value = value.split(//) if value.kind_of?(String) + if value.nil? or value.size < option_value.begin record.errors.add(attr, too_short) - elsif value.split(//).size > option_value.end + elsif value.size > option_value.end record.errors.add(attr, too_long) end end @@ -569,11 +570,8 @@ module ActiveRecord message = (options[:message] || options[message_options[option]]) % option_value validates_each(attrs, options) do |record, attr, value| - if value.kind_of?(String) - record.errors.add(attr, message) unless !value.nil? and value.split(//).size.method(validity_checks[option])[option_value] - else - record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value] - end + value = value.split(//) if value.kind_of?(String) + record.errors.add(attr, message) unless !value.nil? and value.size.method(validity_checks[option])[option_value] end end end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 34d55dbcf6..3d83b8d4d1 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -806,6 +806,20 @@ class ValidationsTest < ActiveRecord::TestCase reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') assert t.valid? end + + def test_validates_size_of_association_using_within + assert_nothing_raised { Topic.validates_size_of :replies, :within => 1..2 } + t = Topic.new('title' => 'noreplies', 'content' => 'whatever') + assert !t.save + assert t.errors.on(:replies) + + reply = t.replies.build('title' => 'areply', 'content' => 'whateveragain') + assert t.valid? + + 2.times { t.replies.build('title' => 'areply', 'content' => 'whateveragain') } + assert !t.save + assert t.errors.on(:replies) + end def test_validates_length_of_nasty_params assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) }