NameError(未初始化的常量,多对多)轨道中的单传递关联

时间:2022-09-29 05:28:28

My many to many associations seem not to be working. An example is this: where TasksTestMethods is the JoinTable

我的很多很多协会似乎都没有工作。一个例子是: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>]>

The join table in schema.rb looks like this

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

Now when I want to query for TestMethods for task with id of 2 as seen above.

现在,当我想查询具有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>

I get the following error when I run it like this

当我像这样运行时,我收到以下错误

task.test_methods
Traceback (most recent call last):
        1: from (irb):6
NameError (uninitialized constant Task::TasksTestMethod)

Same when I try for TestMethod

我尝试使用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">

I get this error:

我收到此错误:

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

I'm getting this same error for all JoinTables in my app. What I'm I doing wrong? And my models are as follows:

我在我的应用程序中为所有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

I also tried to rename the model name from TasksTestMethods to TasksTestMethod based on this error NameError (uninitialized constant Task::TasksTestMethod) but that didn't help.

我还尝试根据此错误NameError(未初始化的常量Task :: TasksTestMethod)将模型名称从TasksTestMethods重命名为TasksTestMethod,但这没有帮助。

Is there anything i'm missing? Thanks in advance for your help.

有什么我想念的吗?在此先感谢您的帮助。

1 个解决方案

#1


1  

Name your rb file containing this class properly, i.e. tasks_test_method.rb.

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

#1


1  

Name your rb file containing this class properly, i.e. tasks_test_method.rb.

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