我使用的是:rails 6.1.4
和ruby 3.1.1p18
我上一次在Rails中使用多对多关联已经有一分钟了,这一次我有点迷茫.如有任何帮助/建议,我们将不胜感激.
如果需要,我很乐意编辑帖子以添加更多信息.
我在我的Rails应用程序living_muay_thai/
中创建了一个目录,并在其中try 在两个模型之间创建many to many
关系.我把所有的代码都准备好了.使用console (development)
,我try 在JOINS表中创建一条记录,但得到一个错误:
3.1.1 :009 > sb.errors.full_messages
=> ["Living muay thai student must exist", "Living muay thai badge must exist"]
我的代码是:
generate models/migrations:
rails generate model living_muay_thai/Student
rails generate model living_muay_thai/Badge
此时,我不记得我是如何创建JOINS表的.我想我try 了几个不同的生成行,但都以索引名的错误太长而结束,所以我最终手动编辑它,以使错误变得令人满意.
Migrations:
Students:
class CreateLivingMuayThaiStudents < ActiveRecord::Migration[6.1]
def change
create_table :living_muay_thai_students do |t|
t.string :fname
t.string :lname
t.timestamps
end
end
end
--------------------------
Badges:
class CreateLivingMuayThaiBadges < ActiveRecord::Migration[6.1]
def up
create_table :living_muay_thai_badges do |t|
t.string :color
t.string :category
t.integer :number
t.timestamps
end
end
def down
drop_table :living_muay_thai_badges
end
end
------------------
StudentBadges (joins table)
class CreateLivingMuayThaiStudentBadges < ActiveRecord::Migration[6.1]
def change
create_table :living_muay_thai_student_badges do |t|
t.integer :student_id
t.integer :badge_id
t.timestamps
end
add_index :living_muay_thai_student_badges, :student_id, name: 'students_index'
add_index :living_muay_thai_student_badges, :badge_id, name: 'badges_index'
end
end
对于学生证迁移,我是这样做的(上图),因为按照几个教程提到的方式做会出错……Index name too long...
左右,这种方式奏效了.
Models:
Students:
# == Schema Information
#
# Table name: living_muay_thai_students
#
# id :bigint not null, primary key
# fname :string
# lname :string
# created_at :datetime not null
# updated_at :datetime not null
#
class LivingMuayThai::Student < ApplicationRecord
has_many :living_muay_thai_student_badges
has_many :living_muay_thai_badges, through: :living_muay_thai_student_badges
has_many :living_muay_thai_levels
end
----------------------
Badges:
# == Schema Information
#
# Table name: living_muay_thai_badges
#
# id :bigint not null, primary key
# category :string
# color :string
# number :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class LivingMuayThai::Badge < ApplicationRecord
has_many :living_muay_thai_student_badges
has_many :living_muay_thai_students, through: :living_muay_thai_student_badges
end
---------------------
StudentBadge
# == Schema Information
#
# Table name: living_muay_thai_student_badges
#
# id :bigint not null, primary key
# badge_id :integer
# student_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# badges_index (badge_id)
# students_index (student_id)
#
class LivingMuayThai::StudentBadge < ApplicationRecord
belongs_to :living_muay_thai_student
belongs_to :living_muay_thai_badge
end
我还没有充实控制器的内容.在此之前,我想在控制台中测试一下.我不确定这和我的问题有什么因果关系.(??)
控制台:
# create a student:
3.1.1 :001 > s1=LivingMuayThai::Student.create(:fname => 'John', :lname=>'Smith')
# works
# create a badge:
3.1.1 :002 > b1=LivingMuayThai::Badge.create(:color => 'White', :category => 'Beginner', :number => 1)
# works
# create record in joins table:
3.1.1 :003 > sb=LivingMuayThai::StudentBadge.create(:student_id => s1.id, :badge_id => b1.id)
=> #<LivingMuayThai::StudentBadge:0x00007f713867a970 id: nil, student_id: 11, badge_id: 140, created_at: nil, updated_at: nil>
3.1.1 :004 > sb.valid?
=> false
3.1.1 :005 > sb.errors.full_messages
=> ["Living muay thai student must exist", "Living muay thai badge must exist"]
再次感谢您的关注.非常感谢.