app/models/taxon_overview.rb
# TODO - should we cache this?  Seems expensive for something that won't change often. It seems simple (to me) to at least
# denormalize the member count on the community model itself, which would save us the bigger query here. ...But that would be
# in addition to caching the results for this overview.
should we cache this.  Seems expensive for something that won't change often. It seems simple (to me) to at least
denormalize the member count on the community model itself, which would save us the bigger query here. ...But that would be
in addition to caching the results for this overview.
should we cache this . Seems expensive for something that won ' t change often . It seems simple ( to me ) to at least denormalize the member count on the community model itself which would save us the bigger query here . ... But that would be in addition to caching the results for this overview .
def communities
    community_ids = taxon_concept.communities.map(&:id).compact
    return [] if community_ids.blank?
    member_counts = Member.select("community_id").group("community_id").where(["community_id IN (?)", community_ids]).
      order('count_community_id DESC').count
    best_three = if member_counts.blank?
      taxon_concept.communities[0..COMMUNITIES_TO_SHOW-1]
                 else
      communities_sorted_by_member_count = member_counts.keys.map { |collection_id| taxon_concept.communities.detect { |c| c.id == collection_id } }
      communities_sorted_by_member_count[0..COMMUNITIES_TO_SHOW-1]
                 end
    Community.preload_associations(best_three, :collections, select: { collections: [:id, :collection_items_count] })
    return best_three
  end
def communities community_ids = taxon_concept . communities . map ( & :id ) . compact return [ ] if community_ids . blank? member_counts = Member . select ( "community_id" ) . group ( "community_id" ) . where ( [ "community_id IN (?)" , community_ids ] ) . order ( 'count_community_id DESC' ) . count best_three = if member_counts . blank? taxon_concept . communities [ 0 .. COMMUNITIES_TO_SHOW - 1 ] else communities_sorted_by_member_count = member_counts . keys . map { | collection_id | taxon_concept . communities . detect { | c | c . id == collection_id } } communities_sorted_by_member_count [ 0 .. COMMUNITIES_TO_SHOW - 1 ] end Community . preload_associations ( best_three , :collections , select : { collections : [ :id , :collection_items_count ] } ) return best_three end
TODO - should we cache this?
TODO - should we cache this?
# communities are sorted by the most number of members - descending order