Permalink
Browse files

Refactor && simplify count_records.

  • Loading branch information...
1 parent e2bad8a commit 9f35799221c8a3c06b3f34a38525654b59598cfd @miloops miloops committed with tenderlove Nov 20, 2010
Showing with 1 addition and 5 deletions.
  1. +1 −5 activerecord/lib/active_record/associations/has_many_association.rb
@@ -42,11 +42,7 @@ def count_records
# documented side-effect of the method that may avoid an extra SELECT.
@target ||= [] and loaded if count == 0
- if @reflection.options[:limit]
- count = [ @reflection.options[:limit], count ].min
- end
-
- count
+ @reflection.options[:limit] ? [@reflection.options[:limit], count].min : count
end
def has_cached_counter?

11 comments on commit 9f35799

Contributor

pahanix replied Nov 23, 2010

[@reflection.options[:limit], count].compact.min

Contributor

miloops replied Nov 23, 2010

@pahanix

I think theres no need to call compact, if options[:limit] exists is not nil and:

[1,nil].compact # => returns 1

So we can avoid that Array#compact call.

Owner

tenderlove replied Nov 23, 2010

I think @pahanix is suggesting to replace the ternary with just [@reflection.options[:limit], count].compact.min

Owner

tenderlove replied Nov 23, 2010

You could even do this: [@reflection.options[:limit] || 0, count].min

Contributor

pahanix replied Nov 23, 2010

[@reflection.options[:limit] || 0, count].min will always return 0

Owner

tenderlove replied Nov 23, 2010

ya, I guess that wouldn't work. ;-)

Contributor

pahanix replied Nov 23, 2010

yes, I meant to replace the ternary operator

Owner

spastorino replied Nov 24, 2010

I guess @tenderlove tried to say ...

[@reflection.options[:limit] || count, count].min
Contributor

miloops replied Nov 24, 2010

@pahanix oh, right!

Then I guess this would work:

[@reflection.options[:limit], count].min

if options[:limit] is nil it will return count. c/d?

Contributor

miloops replied Nov 24, 2010

deny!

[@reflection.options[:limit], count].compact.min would be the way to go :=)

Owner

tenderlove replied Nov 24, 2010

done 0687b21

Please sign in to comment.