Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 由于我很久以前写的迁移的影响,我犯了一个错误_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 由于我很久以前写的迁移的影响,我犯了一个错误

Ruby on rails 由于我很久以前写的迁移的影响,我犯了一个错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想添加新列'piece_id',并将其添加到Bucket表中 class AddPieceIdToBuckets < ActiveRecord::Migration def change add_column :buckets, :piece_id, :string end end class Bucket < ActiveRecord::Base validates :piece_id, presence: true end 虽然我在以前写的迁移中没有p

我想添加新列'piece_id',并将其添加到Bucket表中

class AddPieceIdToBuckets < ActiveRecord::Migration
  def change
    add_column :buckets, :piece_id, :string
  end
end


class Bucket < ActiveRecord::Base
  validates :piece_id, presence: true
end
虽然我在以前写的迁移中没有piece_id,但我认为这是在模型中编写验证的影响。及 我为迁移创建了几个我以前写过的对象。我应该编辑它吗

    def change
        Bucket.reset_column_information
        Bucket.create(
              name: 'test_name',
              about: 'test',
         )
    end

我已经被这一点困扰了很多次,因为这个原因,我不再在迁移中通过模型进行任何数据更改

由于您可以确信创建Bucket的旧迁移已经运行,因此可以删除该代码位,或者更好地将
piece\u id
添加到属性中,并为其指定一个值,以便在从空数据库迁移时通过

我现在使用
rake db:seeds
db/seeds.rb
中创建数据,我有:

require_relative "seeds/#{Rails.env}/all"
以及目录结构,例如:

db/seeds/development/all.rb db/seeds/production/all.rb db/seeds/development/all.rb db/seeds/production/all.rb 在
development/all.rb
中,我确实
require\u relative'../production/all'


作为部署的一部分,我运行了
rake db:seed
,效果很好,只需确保seed是幂等的,所以检查记录是否已经存在,或者使用活动记录方法,如
find\u或\u create\u by

表中是否存在
片段id
列? db/seeds/development/all.rb db/seeds/production/all.rb