Rails Console 如何获取多对多关系中的记录

问题描述 投票:0回答:1

rails 6.1
ruby 3.1.1

我在子目录(命名空间)内有多对多关系

...living_muay_thai
。我有三张桌子:
students
levels
student_levels
。我在每个表中创建了记录。现在我想显示特定学生的级别。我认为命名空间让我感到困惑。

code

Models:

# Table name: living_muay_thai_students
#
#  id         :bigint           not null, primary key
#  fname      :string
#  lname      :string
#
class LivingMuayThai::Student < ApplicationRecord
    has_many :living_muay_thai_student_levels
    has_many :living_muay_thai_levels, through: :living_muay_thai_student_levels, dependent: :destroy
end



# Table name: living_muay_thai_levels
#
#  id         :bigint           not null, primary key
#  color      :string
#  level_name :string
#  sort_order :integer
#
class LivingMuayThai::Level < ApplicationRecord
    has_many :living_muay_thai_student_levels
    has_many :living_muay_thai_students, through: :living_muay_thai_student_levels, dependent: :destroy
end

# joins table

# Table name: living_muay_thai_student_levels
#
#  id         :bigint           not null, primary key
#  level_id   :integer
#  student_id :integer
#
class LivingMuayThai::StudentLevel < ApplicationRecord
  belongs_to :living_muay_thai_student, class_name: 'LivingMuayThai::Student', foreign_key: :student_id
  belongs_to :living_muay_thai_level, class_name: 'LivingMuayThai::Level', foreign_key: :level_id
end

如前所述,我在每个表中都有一条记录:

# living_muay_thai_students
id: 1
fname 'John'
lname 'Smith'

# living_muay_thai_levels
id: 1
color: 'White'
level_name: 'Level 1'
sort_order: 1

# living_muay_thai_student_levels
id: 1
student_id: 1
level_id: 1

现在,在控制台或视图中,如果我有一个学生,如何访问相应的级别记录?

#console:

3.1.1 :001 > student1 = LivingMuayThai::Student.first

3.1.1 :002 > level1 = LivingMuayThai::Level.first

我想做的是(简单的英语):

student1.level_name
,这将是
Level 1

# first try/error:
3.1.1 :003 > student_level = student1.living_muay_thai_levels.level_name
#
# error: `compute_type': uninitialized constant LivingMuayThai::Student::LivingMuayThaiStudentLevel (NameError)  old_raise.call(*args)

2nd try/error:
3.1.1 :004 > student_level = student1.living_muay_thai_student_levels.level_name
#
# error: `compute_type': uninitialized constant LivingMuayThai::Student::LivingMuayThaiStudentLevel (NameError)  old_raise.call(*args)

关于我做错了什么以及我应该如何尝试遍历数据有什么想法吗?

谢谢!

ruby-on-rails many-to-many rails-console ruby-on-rails-6.1
1个回答
0
投票

我首先要正确声明模块并删除所有关联上的可怕的长前缀。

module LivingMuayThai
  class Student < ApplicationRecord
    has_many :student_levels
    has_many :levels, through: :student_levels, 
                      dependent: :destroy
  end
end
module LivingMuayThai
  class Level < ApplicationRecord
    has_many :student_levels
    has_many :students, through: :student_levels, 
                        dependent: :destroy
  end
end
module LivingMuayThai
  class StudentLevel < ApplicationRecord
    belongs_to :student
    belongs_to :level
  end
end
class CreateLivingMuayThaiStudentLevels < ActiveRecord::Migration[7.0]
  def change
    create_table :living_muay_thai_student_levels do |t|
      # We need to explicitly tell Rails which table here
      t.belongs_to :student, null: false, foreign_key: { to_table: :living_muay_thai_students }
      t.belongs_to :level, null: false, foreign_key:  { to_table: :living_muay_thai_levels }

      t.timestamps
    end
  end
end

然后,您犯了一个非常常见的初学者错误,并尝试调用属于整个记录集合上的模型实例的属性方法。

student_level = student1.living_muay_thai_student_levels.level_name

目前还不清楚你到底想在这里完成什么。

如果您尝试向学生添加现有级别,您只需使用铲子操作符即可。

student1.levels << LivingMuayThai::Level.find_by!(name: 'Level 1')

或者,如果您想显示学生的水平,您需要迭代集合:

student1.levels.each {|l| puts l.level_name }
© www.soinside.com 2019 - 2024. All rights reserved.