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
Post a Comment