Similar problem often occurs when I am using "unless", I thought I must ask about it if probably there is a reason or it may be a fault with rails?
I usually handle it by removing "unless" and making it an "if"
if package.services.find_by_service_type_id(service_type_id).present?
package.services.find_by_service_type_id(service_type_id).update_attribute(:total_cost, total) unless service_type_id.nil?
else
puts "*"*80
puts count++
end