Skip to content

Instantly share code, notes, and snippets.

@ianhenrysmith
Last active August 14, 2017 21:37
Show Gist options
  • Select an option

  • Save ianhenrysmith/fbf206117066be7244eb78a97dd00566 to your computer and use it in GitHub Desktop.

Select an option

Save ianhenrysmith/fbf206117066be7244eb78a97dd00566 to your computer and use it in GitHub Desktop.
# mongoid query to get post counts by body type
def body_types
@body_types = PostType.all.distinct(:body_type).sort
end
def body_type_count(body_type)
post_type_ids = PostType.where(body_type: body_type).distinct(:id)
Post.where(:post_type_id.in => post_type_ids, :synced_to_gallery_date.ne => nil).count
end
def body_type_counts
{}.tap { |result| body_types.map { |body_type| result[body_type] = body_type_count(body_type) } }
end
def print_counts
body_type_counts.each do |body_type, count|
puts "#{body_type.humanize}: #{count}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment