简体   繁体   中英

Rails migration add_column with existing data

Using Rails and MYSQL. Existing table has a column called c1, I would like to add a column called c2 with data func(c1), func is the model method to deal with c1 data. How should I write the migration? Thanks

  1. rails generate migration add_something_to_tablename something:string

  2. rake db:migrate

  3. Inside that model

add this line

before_save :do_something

add this method

private
  def do_something
    do something with c1
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM