0

多対多の関係を持つテーブルが必要です。正しいテーブル codesecure_project_tst_definition を作成しましたが、動作します。TstDefinition オブジェクトで codesecure_projects << メソッドを呼び出すことにより、行を結合できます。問題は、何らかの理由でアクティブ レコードが Codesecure_project_id を codesecure_project_tst_definition テーブルの id 値として使用しようとすることです。私は何を間違っていますか?codesecure_projects << メソッドを呼び出したときに、codesecure_project_tst_definition テーブルの ID を設定しようとしないようにするにはどうすればよいですか?

以下に移行を投稿しました

class CreateCodesecureProjects < ActiveRecord::Migration
  def self.up
    create_table :codesecure_projects do |t|
      t.string :name
      t.string :lang

      t.timestamps
    end
  end

  def self.down
    drop_table :codesecure_projects
  end
end


class CreateTstDefinitions < ActiveRecord::Migration
  def self.up
    create_table :tst_definitions do |t|
      t.string :test_name

      t.timestamps
    end
  end

  def self.down
    drop_table :tst_definitions
  end
end


class CreateCodesecureProjectsTstDefinitions < ActiveRecord::Migration
  def self.up
    create_table :codesecure_projects_tst_definitions do |t|
      t.references :codesecure_project
      t.references :tst_definition

      t.timestamps
    end
  end

  def self.down
    drop_table :codesecure_projects_tst_definitions
  end
end

モデルの関連部分:

class TstDefinition < ActiveRecord::Base
  has_and_belongs_to_many :codesecure_projects
  has_many :tst_datas 

class CodesecureProject < ActiveRecord::Base
  has_many :input_abstractions
  has_and_belongs_to_many :tst_definitions
4

1 に答える 1

4

このブログ投稿http://jimcortez.com/blog/?p=9のおかげで、いくつかの検索の後、実際に答えを見つけました。codesecure_projects_tst_definitions テーブルから id 列を削除するだけで済みました。したがって、移行は次のようになります。

class CreateCodesecureProjectsTstDefinitions < ActiveRecord::Migration
  def self.up
    create_table :codesecure_projects_tst_definitions, :id => false do |t|
      t.references :codesecure_project
      t.references :tst_definition

      t.timestamps
    end
  end

  def self.down
    drop_table :codesecure_projects_tst_definitions
  end
end
于 2008-11-03T09:09:02.003 に答える