RAILS 6 has_and_belongs_to_many >> join_table << 不起作用
RAILS 6 has_and_belongs_to_many >> join_table << does not work
我想在两个多对多关系之间使用 join_table。
学生 -> join_table <- 老师
模型定义:
class Student < ApplicationRecord
has_and_belongs_to_many :teachers, join_table: map_student_teacher
end
class Teacher < ApplicationRecord
has_and_belongs_to_many :students, join_table: map_student_teacher
end
迁移定义:
class CreateStudents < ActiveRecord::Migration[6.0]
def change
create_table :students do |t|
t.string : student_name
end
end
end
class CreateTeachers < ActiveRecord::Migration[6.0]
def change
create_table :teachers do |t|
t.string : teacher_name
end
end
end
class CreateStudentsTeachersJoinTable < ActiveRecord::Migration[6.0]
def change
create_table :map_student_teacher, id: false do |t|
t.belongs_to :student, index: true
t.belongs_to :teacher, index: true
end
end
end
现在我有一个 seeds.rb 文件来初始化学生的名字。
students_defaults = ["hans","otto","paul","elsa"]
students_defaults.each do |name|
Student.create(student_name: name)
end
当我通过 rails db:seed
加载种子时
我收到此错误消息:
rails aborted!
NameError: undefined local variable or method `map_student_teacher' for Student (call 'Student.connection' to establish a connection):Class
/home/sven/.rvm/gems/ruby-2.5.3/gems/activerecord-6.0.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
出了什么问题?
在您的模型 类 中,它应该是 join_table: :map_student_teacher
。注意额外的冒号使 map_student_teacher 成为一个符号。没有那个 Ruby 试图查看一个名为 map_student_teacher 的局部变量,它是未定义的。
class Student < ApplicationRecord
has_and_belongs_to_many :teachers, join_table: :map_student_teacher
end
class Teacher < ApplicationRecord
has_and_belongs_to_many :students, join_table: :map_student_teacher
end
如果这对你来说很难看,你也可以使用旧的 :join_table => :map_student_teacher
语法。
我想在两个多对多关系之间使用 join_table。
学生 -> join_table <- 老师
模型定义:
class Student < ApplicationRecord
has_and_belongs_to_many :teachers, join_table: map_student_teacher
end
class Teacher < ApplicationRecord
has_and_belongs_to_many :students, join_table: map_student_teacher
end
迁移定义:
class CreateStudents < ActiveRecord::Migration[6.0]
def change
create_table :students do |t|
t.string : student_name
end
end
end
class CreateTeachers < ActiveRecord::Migration[6.0]
def change
create_table :teachers do |t|
t.string : teacher_name
end
end
end
class CreateStudentsTeachersJoinTable < ActiveRecord::Migration[6.0]
def change
create_table :map_student_teacher, id: false do |t|
t.belongs_to :student, index: true
t.belongs_to :teacher, index: true
end
end
end
现在我有一个 seeds.rb 文件来初始化学生的名字。
students_defaults = ["hans","otto","paul","elsa"]
students_defaults.each do |name|
Student.create(student_name: name)
end
当我通过 rails db:seed
加载种子时
我收到此错误消息:
rails aborted!
NameError: undefined local variable or method `map_student_teacher' for Student (call 'Student.connection' to establish a connection):Class
/home/sven/.rvm/gems/ruby-2.5.3/gems/activerecord-6.0.2.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
出了什么问题?
在您的模型 类 中,它应该是 join_table: :map_student_teacher
。注意额外的冒号使 map_student_teacher 成为一个符号。没有那个 Ruby 试图查看一个名为 map_student_teacher 的局部变量,它是未定义的。
class Student < ApplicationRecord
has_and_belongs_to_many :teachers, join_table: :map_student_teacher
end
class Teacher < ApplicationRecord
has_and_belongs_to_many :students, join_table: :map_student_teacher
end
如果这对你来说很难看,你也可以使用旧的 :join_table => :map_student_teacher
语法。