ruby-on-rails Rails控制台如何获取多对多关系中的记录

dm7nw8vv  于 2023-10-21  发布在  Ruby
关注(0)|答案(1)|浏览(107)

rails 6.1ruby 3.1.1
我在子目录(命名空间)...living_muay_thai中有多对多的关系。我有三张table:studentslevelsstudent_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_nameLevel 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)

我做错了什么,我应该如何尝试遍历数据,有什么想法吗?
谢谢你,谢谢

eulz3vhy

eulz3vhy1#

我会从正确地声明模块开始,并删除所有关联上长得可怕的前缀。

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

这里的广告可能只是看起来像一个风格的选择,但它肯定不是。通过使用module关键字,可以重新打开模块并更改模块嵌套,以便Ruby在LivingMuayThai模块中查找其他常量。
那么您就犯了一个非常常见的初学者错误,试图对整个记录集合调用示例方法。

student_level = student1.living_muay_thai_levels.level_name

我不知道你到底想干什么。
如果你想显示一个学生的水平,你需要覆盖集合:

student1.levels.each {|l| puts l.level_name }

或者,如果你想显示最高水平,你需要得到一个单一的记录:

student1.stundent_levels.last.level_name

相关问题