Browse files

Minor refactoring of validates_associated to replace #inject with #co…

…llect + #all?

[#1686 state:committed]
  • Loading branch information...
1 parent 73cc5f2 commit ccda96093a3bf3fb360f7c6d61bbbf341b2ae034 @joshsusser joshsusser committed with NZKoz Jan 2, 2009
Showing with 1 addition and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/validations.rb
2 activerecord/lib/active_record/validations.rb
@@ -904,7 +904,7 @@ def validates_associated(*attr_names)
validates_each(attr_names, configuration) do |record, attr_name, value|
- unless (value.is_a?(Array) ? value : [value]).inject(true) { |v, r| (r.nil? || r.valid?) && v }
+ unless (value.is_a?(Array) ? value : [value]).collect { |r| r.nil? || r.valid? }.all?
record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)

8 comments on commit ccda960


#all? takes a block as well:

unless (value.is_a?(Array) ? value : [value]).all? { |r| r.nil? || r.valid? }

Wow, the comment is not as seriously meant as github formats it…


GitHub comments needs a preview button for sure…

unless [*value].all? { |r| r.nil? || r.valid? }

The semantics of this method are very specific and won’t work with the all? block. The enumeration must call #valid? on every value to force generation of all errors, then the #all? will return whether the whole set if valid.


ha, didn’t realize i was logged in as pivotal when i typed that previous comment. but i stand by it!


[*value] would be an improvement over (value.is_a?(Array) ? value : [value]), though, both in readability and performance.


Ummm…half my comment disappeared? I was trying to say:

would be an improvement over
(value.is_a?(Array) ? value : [value])
Please sign in to comment.