0
uninitialized constant in blah/blah/add_feed_id_to_entry_states.rb:6:in `up'
class AddFeedIdToEntryStates < ActiveRecord::Migration
  def up
    add_column :entry_states, :feed_id, :integer
    add_index :entry_states, :feed_id

    EntryState.find_each do |entry_state| 
      entry_state.feed_id = entry_state.entry.feed_id
      entry_state.save!
    end
  end  

  def down
    remove_column :entry_states, :feed_id
  end
end

6 行目の何が問題なのか、誰にもわかりますか? Ruby 2.0を使用した「EntryState.find_each」

4

2 に答える 2

0

移行にモデルを含めないでください。移行コードのポイントは、マスターにコミットされると変更されないことです。

モデルを大幅に変更するとどうなるかを考えてみてください EntryState。移行が失敗する可能性があり、データベースをゼロから作成するのは非常に面倒です。それは間違いなく価値がありません。

代わりに、そのようなデータ生成コードをシード ファイルに入れるか、コンソールで実行する必要があります。

しかし、この特定の状況で、デリゲートの使用を考えたことはありますか?

class Feed < ActiveRecord::Base
  has_many :entries
  has_many :entry_states, :through => :entries
end

class Entry < ActiveRescord::Base
  belongs_to :feed
  has_many :entry_states
end

class EntryState < ActiveRecord::Base
  belongs_to :entry

  delegate :feed, :to => :entry, :allow_nil => true
end

何かが欠けていない限り、これで問題は解決します。

于 2013-09-17T04:50:54.163 に答える