Counting array elements in Ruby (unexpected results by the count( ) function) -


in understanding following ruby expressions should produce same result.
apparently missing something, way serious bug go unnoticed...

# returns number of elements in array count = @quotation.quotation_items.count { |x| x.placement == current_placement}  # expect count = (@quotation.quotation_items.find_all { |x| x.placement == current_placement }).length 

quotation_items above activerecord has_many association

#count not take block that.

if want use conditions on count, do:

@quotation.quotation_items.count(:conditions => "placement = #{current_placement}") 

http://apidock.com/rails/activerecord/calculations/classmethods/count


Comments

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -