Ruby on rails 运行rake db:migrate时的Ruby Uninitialized常量 class AddFeedIdToEntryStates
有人能看到6号线出了什么问题吗?“EntryState.find_each”使用ruby 2.0,永远不要尝试在迁移中不包含模型。迁移代码的要点是,一旦将其提交给master,它就不会更改 想想如果您彻底更改了Ruby on rails 运行rake db:migrate时的Ruby Uninitialized常量 class AddFeedIdToEntryStates,ruby-on-rails,ruby,Ruby On Rails,Ruby,有人能看到6号线出了什么问题吗?“EntryState.find_each”使用ruby 2.0,永远不要尝试在迁移中不包含模型。迁移代码的要点是,一旦将其提交给master,它就不会更改 想想如果您彻底更改了EntryState模型会发生什么。您的迁移可能会失败,从头开始创建db将是一个巨大的痛苦。这绝对不值得 相反,您应该将这种数据填充代码放入种子文件中,或者在控制台中运行它 但是,对于这种特定情况,您是否考虑过使用委托 class AddFeedIdToEntryStates < A
EntryState
模型会发生什么。您的迁移可能会失败,从头开始创建db将是一个巨大的痛苦。这绝对不值得
相反,您应该将这种数据填充代码放入种子文件中,或者在控制台中运行它
但是,对于这种特定情况,您是否考虑过使用委托
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
类提要:条目的状态
结束
类条目:entry,:allow\u nil=>true
结束
除非我遗漏了什么,否则这会解决你的问题。这个问题有两种解决方案。或者显式地添加
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
在迁移的顶部,或者添加一个虚拟定义(这样,如果EntryState
在以后的迁移中发生了很大变化,那么迁移仍然可以工作:
require 'entry_state'
class EntryState
您的型号名称是什么,发布您的routes.rb。好吧,我不同意,如果您在结构上更改了数据库,因此您需要随数据库一起更改数据,那么它属于迁移。“从不”这个词可能太强了。但是,我认为您应该尽可能避免将数据填充逻辑与迁移逻辑混合使用。在这种情况下,肯定有更好的选择。同意,避免使用它。但这与数据填充无关:这是种子的用途。但如果您的数据模型发生更改,则必须同时更改现有数据以维护数据模型的完整性。这正是迁移的目的。
require 'entry_state'
class EntryState < ActiveRecord::Base
has_one :entry
end