简体   繁体   English

有人知道创建联接表迁移并在联接表中指定引用列的特定名称的另一种方法吗?

[英]Does anybody know another way to create join table migration and specify a specific name of references columns in join table?

I have these two models: 我有以下两种模型:

 module Studying
  class Student < ApplicationRecord
   has_and_belongs_to_many :instructors,
                        class_name: 'Studying::Instructor',
                        foreign_key: 'studying_student_id',
                        association_foreign_key: 'studying_instructor_id'
  end
end


 module Studying
  class Instructor < ApplicationRecord
   has_and_belongs_to_many :students,
                        class_name: 'Studying::Student',
                        foreign_key: 'studying_instructor_id',
                        association_foreign_key: 'studying_student_id'
  end
end

And for join_table I have generated migration: 对于join_table我已经生成了迁移:

def change
  create_table :studying_instructors_students, id: false do |t|
  t.belongs_to :studying_instructor, index: { name: 'index_instructors_students_on_studying_instructor_id' }
  t.belongs_to :studying_student, index: { name: 'index_instructors_students_on_studying_student_id' }
  end
end

So all is working fine, but the point is, that my senior comrade tells me that I should not use in models things such as: 一切正常,但关键是,我的高级同志告诉我,我不应该在模型中使用以下内容:

  foreign_key:  'studying_instructor_id',

and

  association_foreign_key: 'studying_student_id'  

but instead of these I should use: 但是除了这些,我应该使用:

  foreign_key:  'instructor_id', 

and

  association_foreign_key: 'student_id'

and in the same way in the first model because this is against the convention. 在第一个模型中也是如此,因为这违反了约定。 I do not know how I can do this in this models and in tables (how you already understand tables names in db: studying_instructors and studying_students). 我不知道如何在此模型和表格中做到这一点(您如何理解db中的表格名称:learning_instructors和learning_students)。

Any advice please ? 有什么建议吗?

You can create relation table this way 您可以这样创建关系表

def change
  create_table :studying_instructors_students, id: false do |t|
    t.integer :instructor_id, index: true
    t.integer :student_id, index: true
  end
end

and then you can use foreign_key: 'instructor_id', and association_foreign_key: 'student_id' 然后您可以使用foreign_key: 'instructor_id',association_foreign_key: 'student_id'

If I get this wrong way, please, feel free to address additional questions. 如果我用这种错误的方式,请随时解决其他问题。

I would do something like this: 我会做这样的事情:

class Studying::Student < ApplicationRecord
  has_many :student_instructors, class_name: 'Studying::StudentInstructor'
  has_many :instructors, through: :student_instructors, class_name: 'Studying::Instructor'

end

class Studying::Instructor < ApplicationRecord
  has_many :student_instructors, class_name: 'Studying::StudentInstructor'
  has_many :students, through: :student_instructors, class_name: 'Studying::Instructor'      
end

class Studying::StudentInstructor < ApplicationRecord
  belongs_to :student, class_name: 'Studying::Student'
  belongs_to :instructor, class_name: 'Studying::Instructor'      
end

I have three tables here. 我这里有三张桌子。 One for students, second for instructors and third one is a junction table (student_instructors). 一个用于学生,第二个用于教师,第三个用于联结表(student_instructors)。

Migration for studnt_instructor will look something like this 为studnt_instructor进行的迁移看起来像这样

def change
  create_table :student_instuctors, id: false do |t|
    t.integer :instructor_id, index: true
    t.integer :student_id, index: true
  end
end

I have not verified the syntax but this is more of logic explanation. 我没有验证语法,但这更多是逻辑解释。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM