Rails 3 Finding and Grouping over multiple tables with at least one matching parameter

I have a database of Users. Users have Skills and Interests which relate to the same Tags table. I need to create a search query that takes an array of Tags and finds all users with at least one of those tags as a Skill, ordered by largest number found.

From the little bit of SQL I know, I'm thinking:

  1. join the users table with the skills (tags) table
  2. filter by tags that are within the provided array
  3. then group by user id
  4. add a having > 0 on the count

and returning that result. The problem is I can't figure out how to do that with Rails.

So if someone could say whether this is the correct method or not, and how to do so in Rails, I'd much appreciate it.

#User Model
class User < ActiveRecord::Base
    has_and_belongs_to_many :skills, :class_name => "Tag", :join_table => "skills_users"
    has_and_belongs_to_many :interests, :class_name => "Tag", :join_table => "interests_users"

#Tag Model
class Tag < ActiveRecord::Base
    has_and_belongs_to_many :skilled_users, :class_name => "User", :join_table => "skills_users"
    has_and_belongs_to_many :interested_users, :class_name => "User", :join_table => "interests_users"


I did find this, which would work but I feel like its a hack around method for solving this. Rails finding all posts with certain tags without using acts_as_taggable

Update 2

User.find(:all, :joins => :skills, :conditions => { :tags => { :id => [1,2,3] }})

I was able to get a list of all users who had any of the skills passed in. This mean duplicate rows. Unfortunately when I grouped by user id, they didn't group because the Tag information was attached to the row. I also haven't figure out how to count the tags.


I'll answer my own question just in case others come looking for the same type of problem.

users = User.find(:all, :include => :skills, :conditions => { :tags => { :id => [1, 2, 3] } } )

This loads only the users with skills in the id range, and only those skills. Now I can sort by the size of their skills and return.

sorted = users.sort {|a,b| b.skills.size <=> a.skills.size}

Need Your Help

How to set a starting viewcontroller.xib in iOS using objective c

ios objective-c uinavigationcontroller xib appdelegate

I am new to iOS. I create a single view application. Its have one view controller. When compile the application its show the fully black colour of my view controller. Now what I need is how to init...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.