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

  • Home
  • SEARCH
  • 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 3607268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:21:16+00:00 2026-05-18T21:21:16+00:00

In RoR3, I have Users and Skills and each skill is created by a

  • 0

In RoR3,

I have Users and Skills and each skill is created by a user. I wanted to record that, so I created a one to many relationship.

class User < ActiveRecord::Base
  has_many :skills
end

class Skill < ActiveRecord::Base
  belongs_to :user
end

However, each user also has many skills in the sense that, user “Bob” created skill “Kung Fu”, user “Charlie” created skill “Karate” and user “Bob” both created and is able to do both “Kung Fu” and “Karate”

How should I represent this with ActiveRecord? Should I just create a new table “user_skills” which has_many :skills? and belong_to :user?

  • 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-18T21:21:17+00:00Added an answer on May 18, 2026 at 9:21 pm

    There are two different associations here. The first is a one-to-many association. An user can be the creator of any number of skills. The second one is a many-to-many association, an user can have many skills and a skill can have many users.

    The first one is a simple belongs_to <-> has_many declaration. For the second one, you either need a has_and_belongs_to_many declaration in both models, and a related join table, or a dedicated join model, and a has_many :through declaration. Let’s try the first one:

    Method 1: HABTM

    class User < ActiveRecord::Base
      has_many :created_skills, :class_name => 'Skill', :inverse_of => :creator
      has_and_belongs_to_many :skills
    end
    
    class Skill < ActiveRecord::Base
      belongs_to :creator, :class_name => 'User', :inverse_of => :created_skills
      has_and_belongs_to_many :users
    end
    

    This requires a join table called “skills_users” that has columns named user_id and skill_id

    Method 2: Has many through (Join model)

    The second one is similar, but adds a model that acts as the middleman. This has an added benefit that you can include additional columns in the join model, like for example a skill level.

    class User < ActiveRecord::Base
      has_many :created_skills, :class_name => 'Skill', :inverse_of => :creator
      has_many :user_skills
      has_many :skills, :through => :user_skills
    end
    
    class Skill < ActiveRecord::Base
      belongs_to :creator, :class_name => 'User', :inverse_of => :created_skills
      has_many :user_skills
      has_many :users, :through => :user_skills
    end
    
    class UserSkill < ActiveRecord::Base
      belongs_to :user
      belongs_to :skill
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two RoR3 application: http://users.domain.local http://profiles.domain.local I created the 'users/models/profile.rb': class Profile <
In my RoR3 application I have a namespace called NS1 so that I have
I have a model (Entries) with five years worth of records (one record per
I am building a RoR3 site that generates a microsite for each client. At
I am using single table inheritance in ROR3 so I created these models: class
I have a RoR3 server deployed to Heroku (With some webservices). I have a
I am using Maruku with my RoR3 app. But the problem is that when
I am trying to add a new page to my RoR3 application that should
I am trying to implement REST APIs, so in my RoR3 application I have
We have a website, and want to allow users on the site to chat

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.