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 9148743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:21:23+00:00 2026-06-17T11:21:23+00:00

I am working on a User model, and each user should be able to

  • 0

I am working on a User model, and each user should be able to have both students and teachers. However, since student and teacher are both a type of User, my model got a little complicated.

This is what I am trying right now.

Teacher_student_link

class TeacherStudentLink < ActiveRecord::Base
  attr_accessible :student_id, :teacher_id, :user_id

  belongs_to :user
  belongs_to :teacher, :class_name => "User"
  belongs_to :student, :class_name => "User"
end

User

class User < ActiveRecord::Base
  has_many :teacher_student_links, :foreign_key => { :student_id, :teacher_id }, :dependent => :destroy
  has_many :students, :through => :teacher_student_links
  has_many :teachers, :through => :teacher_student_links
end

If everything works as I intended, I should be able to do

@user = User.new
@user.students
@user.teachers
@user.student.teachers

I think the only problem with above is that I can’t give two foreign keys at the same time to teacher_student_link, but I’m not sure. As a workaround, I don’t have teacher_id in my model yet, and just doing student.user to call the teacher. Can anyone help me with this issue?

Update:
With the solution below, how should I create a new link?

def become_student
  @user = user.find(params[:id])
  @student_link = @user.student_links.create(:student_id => current_user.id)
  @teacher_link = current_user.teacher_links.create(:teacher_id => @user.id)
end

If I do it like this, is the student and teacher paired up correctly?
I am confused a little because in the TeacherStudentLink, there are user, student, teacher, and I’m not sure how to deal with creating links.

  • 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-06-17T11:21:27+00:00Added an answer on June 17, 2026 at 11:21 am

    You should separate out the teacher_student_links association into two associations:

      has_many :teacher_links, :foreign_key => :student_id, :dependent => :destroy, :class_name => "TeacherStudentLink"
      has_many :student_links, :foreign_key => :teacher_id, :dependent => :destroy, :class_name => "TeacherStudentLink"
      has_many :students, :through => :student_links
      has_many :teachers, :through => :teacher_links
    

    You might need to add the foreign keys to the belongs_to association on TeacherStudentLink also

    Update:

    Regarding your second question about creating links, the following should work:

    @user = User.find(params[:id])
    @user.students << current_user
    

    The TeacherStudentLink should be created automatically, and your associations should work if everything is set up correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with Rails 3.2.2. And I have a devise model called user. In
The following produces a working select drop down that pulls from my user model:
I am working with a Django model that looks like this: class Subscription(models.Model): user
Background: working a user q&a site where users logged,etc. What user/visitor data should be
In my application I have an extended User model called UserProfile. This user can
I am working on a Rail webapp. I have two models, User, which contains
I have Post and Comment model. I want to achieve the following: Each time
Working with Django 1.2 I am making a wine review site. A user should
I have a User model and Posts model. I want to display the posts
I am trying to finish a project. I am working with user models. When

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.