创建子类之间的关联(STI)

时间:2016-06-19 23:58:13

标签: ruby-on-rails ruby-on-rails-4

我有一个用户类和子类Student和Teacher(使用STI)。

我想在学生和教师之间创建一个has_and_belongs_to_many关联。请让我知道如何创建连接表来完成此关联。

谢谢!

1 个答案:

答案 0 :(得分:0)

迁移:

        char[] ar = { 'a', 'h', 'm', 'a', 'd' };
        char[] ay = { 'a', 'y', 'm', 'a', 'd' };
        List<char> matches = new List<char>();

        if (ar.SequenceEqual(ay))
        {
            Console.WriteLine("Success");
        }
        else
        {
            for (int i = 0; i < ar.Count(); i++)
            {
                if (ar[i] != ay[i])
                {
                    matches.Add(ay[i]);
                }
            }
            foreach (var matched in matches)
            {
                Console.WriteLine("Failure: " + matched);
            }
        }

教师模型:

class CreateStudentsTeachersJoinTable < ActiveRecord::Migration
  def change
    create_table :students_teachers, id: false do |t|
      t.integer :student_id
      t.integer :teacher_id
    end
  end
end

学生模特:

class Teacher < User
  has_and_belongs_to_many :students, join_table: :students_teachers
end