【问题标题】:Rails 5 Could not find the association for join table, test looking for inverse table nameRails 5找不到连接表的关联,测试寻找反向表名
【发布时间】:2019-07-05 00:21:24
【问题描述】:

我有 3 个关于 has_many :through 关系的问题,以及一些我不明白我正在尝试创建的设置的错误。首先,我将描述我的设置:

我有一个任务模型

class Task < ApplicationRecord
  has_many :task_standards
  has_many :standards, through: :task_standards
end

和标准模型

class Standard < ApplicationRecord
  has_many :task_standards
  has_many :tasks, through: :task_standards
end

我需要任务和标准之间的关系,并且还具有另一个字段,因此我生成了一个 TaskStandard 模型

class TaskStandard < ApplicationRecord
  belongs_to :task
  belongs_to :standard
end

创建了迁移20190704213323_create_task_standards.rb

class CreateTaskStandards < ActiveRecord::Migration[5.2]
  def change
    create_table :task_standards do |t|
      t.belongs_to :task, index: true
      t.belongs_to :standard, index: true
      t.string :level

      t.timestamps
    end
  end
end

我可以看到数据库中的task_standards表

                                         Table "public.task_standards"
   Column    |            Type             | Collation | Nullable |                  Default                   
-------------+-----------------------------+-----------+----------+--------------------------------------------
 id          | bigint                      |           | not null | nextval('task_standards_id_seq'::regclass)
 task_id     | bigint                      |           |          | 
 standard_id | bigint                      |           |          | 
 level       | character varying           |           |          | 
 created_at  | timestamp without time zone |           | not null | 
 updated_at  | timestamp without time zone |           | not null | 
Indexes:
    "task_standards_pkey" PRIMARY KEY, btree (id)
    "index_task_standards_on_standard_id" btree (standard_id)
    "index_task_standards_on_task_id" btree (task_id)

我运行了我的测试,但我得到了错误: ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "standards_tasks" does not exist。我已经删除并创建了我的测试和开发环境数据库,然后回滚并重新运行迁移。

我去了 Rails 控制台并尝试创建一个任务,并将任务标准设置为一系列新任务,但控制台给出了错误,ActiveRecord::HasManyThroughAssociationNotFoundError (Could not find the association :standard_tasks in model Task) ... 如果我尝试创建一个标准并且向它添加任务,我得到相同的,但相反:(Could not find the association :task_standards in model Standard)

我的问题是:

  1. 为什么测试寻找standards_tasks 而不是tasks_standards?
  2. 如何在我的控制器中创建 standard.tasks 或 task.standards?我必须创建一个标准任务对象吗?
  3. 我现在知道答案了...为什么我在控制台中得到could not find the association :standard_tasks in model Task 我还没有在我的standard.rb 和task.rb 中添加has_many :task_standards。我所拥有的只是has_many :standards, through :task_standards (doh!)

更新: 我发现我的测试中“standards_tasks”的问题来自哪里:我有一个旧模型生成,它在夹具中创建了standards_tasks.yml,当我销毁该迁移/模型时它没有被删除

【问题讨论】:

  • 有点不清楚你还有什么问题,你似乎至少解决了一些问题?
  • 是的,我想我应该关闭它

标签: ruby-on-rails activerecord ruby-on-rails-5


【解决方案1】:

测试 (ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "standards_tasks" does not exist) 的问题在于先前模型生成被破坏后留下的残留文件。测试/夹具中有一个standards_tasks.yml 文件。当我删除该 .yml 文件时,测试按预期运行。

控制台中could not find the association :standard_tasks in model Task 的问题是由于我所关联的模型中未包含关系表本身造成的。虽然我有has_many :tasks, through :task_standards,但我还没有在我的standard.rb 和task.rb 中添加has_many :task_standards

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    相关资源
    最近更新 更多