Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6866049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:07:53+00:00 2026-05-27T03:07:53+00:00

I have a database of Users. Users have Skills and Interests which relate to

  • 0

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"
end

#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"
end

Update

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:07:54+00:00Added an answer on May 27, 2026 at 3:07 am

    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}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database where users can search for records that have one or
I have a database which users should not be able to alter data in
I have an old database of users which contains passwords hashed in a way
I have a database table where users can filter a single column using a
I have a large database of users (~200,000) that I'm transferring from a ASP.NET
I have a mysql database of users that can login to my site and
I have a query that gets all the info I need for a messaging
I have a table created with PHP from an Sql Database that successfully displays
I have a table for users. Each user has certain skills they teach. So
I have a database containing users and roles. The table Users has a reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.