Update a column, all rows

前端 未结 4 1780

I added a new column to my table but I forgot to add the :default option. Now I want to populate that column on every single row.

Is there a way to do with using the con

4条回答
  •  独厮守ぢ
    2021-01-31 02:06

    Since you already created the new field in a previous migration, create a brand new migration:

    rails g migration UpdateFoos
    

    Modify the migration:

    def self.up    
      say_with_time "Updating foos..." do
        Foo.find(:all).each do |f|
          f.update_attribute :myattribute, 'value'
        end
      end
    end
    
    # from command line
    Rake db:migrate
    

    Let me know if this works, it might need a few adjustments. See rails docs for more:

提交回复
热议问题