我在迁移过程中遇到了基本rails问题。以下是的两个脚本
class CreateGoogleMaps < ActiveRecord::Migration
def self.up
create_table :google_maps do |t|
t.string :name, :null => false
t.string :description
t.column "center", :point, :null => false, :srid => 4326, :with_z => false # 4326: WSG84
t.integer :zoom
t.datetime :created_at
t.datetime :updated_at
t.integer :created_by_id
t.integer :updated_by_id
end
end
def self.down
drop_table :google_maps
end
end
文件#2+++003_add_map_style.rb+++++
class AddMapStyle < ActiveRecord::Migration
def self.up
add_column :google_maps, :style, :integer
GoogleMaps.update_all( "style = 1")
end
def self.down
remove_column :google_maps, :style
end
end
***********************************************
以下是我在迁移过程中看到的情况===创建谷歌地图:移植===============================================--create_table(:google_maps)->0.0638秒===创建谷歌地图:已迁移(0.0640s)==========================
===CreateMarkers:移植==================================================--create_table(:标记)->0.0537秒===CreateMarkers:已迁移(0.0539秒)=================================
==AddMapStyle:移植====================================================--add_column(:google_maps,:style,:integer)->0.0406秒rake中止!出现错误,所有以后的迁移都被取消:
未初始化的常量AddMapStyle::GoogleMaps
我使用的是Rails 2.3.11。非常感谢任何调试技巧!
您可以在迁移中安全地使用模型,例如:
class AddMapStyle < ActiveRecord::Migration
class GoogleMaps < ActiveRecord::Base; end
def self.up
add_column :google_maps, :style, :integer
GoogleMaps.update_all( "style = 1")
end
def self.down
remove_column :google_maps, :style
end
end
由于该类是在迁移类内部定义的,因此它位于一个单独的命名空间中。
您不应该在迁移中使用模型,因为这很危险-模型可能会更改,您试图加载ActiveRecord对象,同时从它们下面更改模式。
如果可以的话,您应该使用SQL,就像下面运行原始更新命令的示例一样。
class AddMapStyle < ActiveRecord::Migration
def self.up
add_column :google_maps, :style, :integer
execute("UPDATE google_maps SET style=1")
end
def self.down
remove_column :google_maps, :style
end
end