Rails,模型关系child-parent

时间:2017-08-19 12:17:41

标签: ruby-on-rails database-design

我有一个简单的应用程序,我必须将学生与父母之间的关系描述为多对多关系,以下内容将起作用:

  

current_user.parents

     

current_user.children

目前我有以下

class User < ApplicationRecord
  has_many :parent_students
  has_many :students, through: :parent_students, class_name: "User"
  has_many :parents,  through: :parent_students, class_name: "User"
end

class ParentStudent < ApplicationRecord
    belongs_to :student, class_name: "User", foreign_key: "student_id"
    belongs_to :parent,  class_name: "User", foreign_key: "parent_id"
end

class CreateParentStudents < ActiveRecord::Migration[5.1]
  def change
    create_table :parent_students do |t|
        t.references :student, index: true, references: :users
        t.references :parent,  index: true, references: :users

      t.timestamps
    end
  end
end

知道这有用吗?谢谢!

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助,用户

中的声明
state
相关问题