NameError(未初始化常量多对多)轨道

时间:2018-05-15 07:41:25

标签: ruby-on-rails

我的许多协会似乎没有工作。 一个例子是:TasksTestMethods是JoinTable

TasksTestMethods.all 
TasksTestMethods Load (0.5ms)  SELECT  "tasks_test_methods".* FROM "tasks_test_methods" LIMIT $1  [["LIMIT", 11]]
 => #<ActiveRecord::Relation [#<TasksTestMethods task_id: 2, test_method_id: 1>]>

schema.rb中的连接表如下所示

create_table "tasks_test_methods", id: false, force: :cascade do |t|
    t.bigint "task_id", null: false
    t.bigint "test_method_id", null: false
    t.index ["task_id", "test_method_id"], name: "index_tasks_test_methods_on_task_id_and_test_method_id"
    t.index ["test_method_id", "task_id"], name: "index_tasks_test_methods_on_test_method_id_and_task_id"
  end

现在,当我想查询具有id为2的任务的TestMethods时,如上所示。

task = Task.last
Task Load (0.9ms)  SELECT  "tasks".* FROM "tasks" ORDER BY "tasks"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => #<Task id: 2, name: "", description: "", start_date: "2018-05-15", end_date: nil, order_number: "", material_type: nil, client_id: 1, drawing_number: nil, task_status_field: nil, invoice_number: nil, offshore_flag: 0, client_ref: "", contact_person: "", contact_person_phone: "", contact_person_email: "", testsite: "", information_to_operator: nil, client_online_order: nil, start_date_asap: nil, thickness: nil, testextent: nil, groove: nil, material_quality: nil, class_society: nil, welder_id: nil, project_id: nil, operator_id: nil, task_manager_id: 1, department_id: 5, created_at: "2018-05-14 22:13:08", updated_at: "2018-05-14 22:18:30", language_id: 2>

当我像这样运行

时出现以下错误
task.test_methods
Traceback (most recent call last):
        1: from (irb):6
NameError (uninitialized constant Task::TasksTestMethod)

当我尝试TestMethod

时也一样
test_method = TestMethod.last
  TestMethod Load (0.4ms)  SELECT  "test_methods".* FROM "test_methods" ORDER BY "test_methods"."id" DESC LIMIT $1  [["LIMIT", 1]]
 => #<TestMethod id: 1, name: "Visual Testing (VT)", code: nil, description: nil, category_id: 1, procedure_id: 1, created_at: "2018-05-15 07:06:51", updated_at: "2018-05-15 07:06:51">

我收到此错误:

test_method.tasks
Traceback (most recent call last):
        1: from (irb):9
NameError (uninitialized constant TestMethod::TasksTestMethod)

我在我的应用中为所有JoinTables收到同样的错误。 我做错了什么?我的模型如下:

class Task < ApplicationRecord
  has_many :tasks_test_methods, :dependent => :destroy 
  has_many :test_methods, :through => :tasks_test_methods
end 

class TestMethod < ApplicationRecord
  belongs_to :category
  belongs_to :procedure

  has_many :tasks_test_methods
  has_many :tasks, :through => :tasks_test_methods
end

class TasksTestMethods < ApplicationRecord 
  belongs_to :task
  belongs_to :test_method
end

我还尝试根据此错误TasksTestMethods将模型名称从TasksTestMethod重命名为NameError (uninitialized constant Task::TasksTestMethod),但这并没有帮助。

我有什么遗失的吗?

1 个答案:

答案 0 :(得分:1)

正确命名包含此类的rb文件,即tasks_test_method.rb