You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 6.1.4中多对多模型通过控制台创建关联记录失败

Rails 多对多关联创建关联记录失败问题

环境

  • rails 6.1.4
  • ruby 3.1.1p18

问题

很久没处理Rails的多对多关联,现在在项目里创建了living_muay_thai/目录,给两个模型配置多对多关系后,在开发控制台创建关联表记录时失败,错误信息:

3.1.1 :009 > sb.errors.full_messages
 => ["Living muay thai student must exist", "Living muay thai badge must exist"]

代码详情

生成模型/迁移文件

rails generate model living_muay_thai/Student
rails generate model living_muay_thai/Badge

用教程方法创建关联表时遇到Index name too long...错误,手动编辑迁移文件解决了这个问题。

迁移文件

学生模型迁移

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

徽章模型迁移

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

学生-徽章关联表迁移

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

模型代码

学生模型

# == 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

徽章模型

# == 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

学生-徽章关联模型

# == 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

控制台操作

# 创建学生记录:
3.1.1 :001 > s1=LivingMuayThai::Student.create(:fname => 'John', :lname=>'Smith')
# 创建成功

# 创建徽章记录:
3.1.1 :002 > b1=LivingMuayThai::Badge.create(:color => 'White', :category => 'Beginner', :number => 1)
# 创建成功

# 创建关联表记录:
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"]

目前还没写控制器代码,想先在控制台验证关联功能,不确定是否和当前问题有关,求解决思路。


内容的提问来源于stack exchange,提问作者John Cowan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 06:36:06