Skip to content

Instantly share code, notes, and snippets.

@dudo
Last active January 19, 2018 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudo/9ee6d7a96b351edd188b to your computer and use it in GitHub Desktop.
Save dudo/9ee6d7a96b351edd188b to your computer and use it in GitHub Desktop.
class items
has_many :tags, through: :item_tags
has_many :item_tags
belongs_to :city
end
class cities
has_many_ :items
end
class tags
has_many :items, through: :item_tags
has_many :item_tags
def top_five_cities
h = {}
self.items.each do |i|
h[i.city] ||= 1
h[i.city] += 1
end
# sort will arrange our cities in ASC order, reverse it and take the first five... (would like to use .first(5) here)
top_five = h.sort_by{|k,v| v}.reverse[0..5]
cities = top_five.keys.map(&:to_s)
end
end
class item_tags # join table
belongs_to :tags
belongs_to :items
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment