0

我正在尝试使用:has_many :through类型关联,但出现以下错误:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: work_units.developer_id:

关于这类事情的许多其他帖子都只是有拼写错误,但我检查了我的。

class Developer < ActiveRecord::Base
  attr_accessible :skype_name, :language_ids, :user_attributes

  has_many :work_units
  has_many :projects, :through => :work_units
  ...
end

class Project < ActiveRecord::Base
  attr_accessible :complete, :description, :finalised, :price

  has_many :work_units
  has_many :developers, :through => :work_units
  ...
end

class WorkUnit < ActiveRecord::Base
  attr_accessible :hours_worked

  belongs_to :project
  belongs_to :developer
end

我跑db:migrate了,它没有抱怨。我确实犯了一个错误,不得不回滚数据库然后重新迁移,但我认为我做对了。我使用了annotategem,它没有显示我期望的任何关系 ID。那么,我需要创建一个 WorkUnits 表还是我遗漏了什么?导轨指南没有提到手动制作表格。

编辑

这是我用来创建 WorkUnit 模型和东西的迁移:

class CreateWorkUnits < ActiveRecord::Migration
  def change
    create_table :work_units do |t|
      t.integer :hours_worked, :default => 0
      t.timestamps
    end
  end
end

编辑 2

我的 schema.rb 中的片段:

create_table "work_units", :force => true do |t|
  t.integer  "hours_worked", :default => 0
  t.datetime "created_at",                  :null => false
  t.datetime "updated_at",                  :null => false
end

create_table "projects", :force => true do |t|
  t.string   "description"
  t.decimal  "price",       :precision => 8, :scale => 2
  t.boolean  "complete",    :default => false
  t.datetime "created_at",  :null => false
  t.datetime "updated_at",  :null => false
end

同样对于:developers。那么,为什么我的迁移没有为我添加关联信息?

4

4 回答 4

3

您的 WorkUnit 迁移应如下所示:

class CreateWorkUnits < ActiveRecord::Migration
  def change
    create_table :work_units do |t|
      t.integer :hours_worked, :default => 0
      t.references :developer
      t.references :project
      t.timestamps
    end
    add_index :work_units, :developer_id
    add_index :work_units, :project_id
  end
end
于 2013-05-09T20:13:07.283 回答
2

您需要将外键添加到work_units表中。

class CreateWorkUnits < ActiveRecord::Migration
  def change
    create_table :work_units do |t|
      t.integer :hours_worked, :default => 0
      t.integer :project_id, null: false
      t.integer :developer_id, null: false
      t.timestamps
    end

    add_index :work_units, :project_id
    add_index :work_units, :developer_id
  end
end

另一种方式:

class CreateWorkUnits < ActiveRecord::Migration
  def change
    create_table :work_units do |t|
      t.integer :hours_worked, :default => 0
      t.belongs_to :project
      t.belongs_to :developer
      t.timestamps
    end

    add_index :work_units, :project_id
    add_index :work_units, :developer_id
  end
end

您还可以在生成模型时定义这些字段,然后它们将自动添加到迁移中,如第二个片段所示。

$ rails g model WorkUnit hours_worked:integer project:belongs_to developer:belongs_to

希望有帮助。

于 2013-05-09T20:12:56.387 回答
0

A table for WorkUnit needs to exist, whether that means it migration was automatically generated via scaffolding or if the migration was manually written by you.

If you don't have a migration yet that creates that table, you'll need to create that migration because the table does need to exist.

于 2013-05-09T20:09:06.547 回答
0

您确实需要一个带有 project_id 和 developer_id 列的 work_units 表。

如果您不知道如何创建表,请查看http://xyzpub.com/en/ruby-on-rails/3.2/activerecord_datenbank_anlegen.html 。

于 2013-05-09T20:13:45.450 回答