Ruby on Rails:模型与多个外键关联 [英] Ruby on Rails: Model Association with multiple foreign keys

查看:219
本文介绍了Ruby on Rails:模型与多个外键关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 User 模型,每个用户都应该能够同时拥有学生和教师。但由于 教师都是 User 的类型,我的模型有点复杂。



这是我现在想尝试的。



Teacher_student_link
$ b

  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

用户 b
$ b

  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

如果一切都按我的意图工作,我应该能够

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

我认为上面唯一的问题是我不能给两个外国人键同时 teacher_student_link ,但我不确定。作为解决方法,我没有在我的模型中的teacher_id,只是 student.user 调用老师。任何人都可以帮助我解决这个问题?



更新:
使用以下解决方案如何创建新链接? / p>

  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

如果我这样做,是学生和老师配对正确吗?
我困惑一点,因为在TeacherStudentLink中有用户学生,<$ c $

解决方案

您可以通过以下方式应将teacher_student_links关联分成两个关联:

  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

您可能需要将外键添加到TeacherStudentLink上的belongs_to关联



更新:



关于创建链接的第二个问题, p>

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

应该自动创建TeacherStudentLink,如果一切设置正确,您的关联应该工作。 / p>

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.

解决方案

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.

这篇关于Ruby on Rails:模型与多个外键关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆