0

I'm trying to order Tags by order of the descending frequency of their association with Users of a specific Group. (ActiveRecord & Rails 3.2 - I also have Squeel installed if that helps!)

Users have Tags, Groups have Users. Groups thus have Tags through Users.

class Group < ActiveRecord::Base
  has_many :users_groups  # join table
  has_many :users, through: :users_groups
  has_many :tags, through: :users

class User < ActiveRecord::Base
  has_many :users_groups  # join table
  has_many :groups, through: :users_groups
  has_many :taggings      # join table
  has_many :tags, through: taggings

class Tag < ActiveRecord::Base
  has_many :taggings      # join table
  has_many :users, through: taggings

These answers from Steve [ https://stackoverflow.com/a/11624005/59195 ] and Amol [ https://stackoverflow.com/a/10958311/59195 ] come closest to what I'm trying to do — though I want to order the tags only by their frequency of use/association by users who are members of the group, not all users.

Any ideas? Thank you for your help!

Community
  • 1
  • 1
djoll
  • 1,139
  • 1
  • 12
  • 31
  • If `@group` is a specific group, what does `@group.tags.order` return? – dax Oct 23 '13 at 09:08
  • Hey dax! It runs this SQL (NB: taggings is polymorphic...): SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" INNER JOIN "users" ON "taggings"."taggable_id" = "users"."id" AND "taggings"."taggable_type" = 'User' INNER JOIN "users_groups" ON "users"."id" = "users_groups"."user_id" WHERE "users_groups"."group_id" = 1 – djoll Oct 23 '13 at 09:38
  • and does that generate anything useful for you? basically you're selecting tags from group and ordering them here - is that what you want? – dax Oct 23 '13 at 10:03
  • It appropriately returns all the Tags associated with a group through Users who are a member of @group. By default this is unordered (ie. by ID) although I have a scope [ scope :alphabetical, order("name ASC") ] I use for alphabetical ordering. What I am trying to get is a descending list of the most frequently used tags for a group of users (rather than all users, as Steve's answer referenced above, provided). – djoll Oct 23 '13 at 10:29
  • hey @dj :) could you please mention about your so far attempt to solve this or any pseudo code. Or what I am looking for is a simplest(easy to understand) version of your question. Or no big deal if you could achieve what you want in more than two lines, code would be readable – Amol Pujari Oct 23 '13 at 11:32
  • Thanks Amol! Pseudocode would be `some_group.tags.order_by(count where user.users_group == some_group)` ie. show me the tags of all users of some_group listed in order of most to least frequently used tags _for users who are members of some_group_. Your answer `Tag.joins(:taggings).select('tags.*, count(tag_id) as "tag_count"').group(:tag_id).order(' tag_count desc')` seems more than half-way there but I can't work out how to adapt it to the 'groups of users' situation. – djoll Oct 23 '13 at 12:45

0 Answers0