0

Ruby on Railsで問題が発生しています。具体的には、deal_eventを介してdealおよびeventとの多対多の接続を設定しています。私はいくつかの同様のstackoverflowの質問、さらにはhttp://guides.rubyonrails.org/をチェックしましたが、まだ何かを得ていません。

これが私のモデルです:

Deal.rb

class Deal < ActiveRecord::Base
  has_many :deal_events
  has_many :events, :through => "deal_events"
  attr_accessible :approved, :available, :cents_amount, :dollar_amount, :participants, :type
end

event.rb

class Event < ActiveRecord::Base
  has_many :deal_events
  has_many :deals, :through => "deal_events"
  attr_accessible :day, :image, :description, :location, :title, :venue, :remove_image
end

Deal_event.rb

class DealEvent < ActiveRecord::Base
  belongs_to :deal
  belongs_to :event
end

そして、これが私の移行ファイルです:

20130102150011_create_events.rb

class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.string :title,     :null => false
      t.string :venue
      t.string :location
      t.text :description 
      t.date :day

      t.timestamps
    end
  end
end

20130112182824_create_deals.rb

class CreateDeals < ActiveRecord::Migration
  def change
    create_table :deals do |t|
      t.integer :dollar_amount
      t.integer :cents_amount
      t.integer :participants
      t.string  :type, :default => "Deal"
      t.integer :available
      t.string  :approved

      t.timestamps
    end
  end
end

20130114222309_create_deal_events.rb

class CreateDealEvents < ActiveRecord::Migration
  def change
    create_table :deal_events do |t|
      t.integer :deal_id, :null => false
      t.integer :event_id, :null => false

      t.timestamps
    end
  end
end

1つの取引と1つのイベントをデータベースにシードした後、コンソールに移動して入力します

deal = Deal.first # ok
event = Event.first # ok

DealEvent.create(:deal => deal, :event => event) # Error: ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: deal, event

deal.events # Error: ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association "deal_events" in model Deal

これらの2つのエラーがポップアップするために私が間違っていることについて何か考えはありますか?ありがとう。

4

1 に答える 1

1

DealEventモデルには次の行が必要です。

attr_accessible :deal, :event

それが単なるリレーションシップテーブル(そのように見える)である場合でも、そのようにリレーションシップを作成することはありません。ネストされたフォームなどを使用します。

于 2013-01-15T02:33:09.190 に答える