Search code examples
ruby-on-railssqliteuppercaselowercase

Changing data in sqlite3 db used by Rails


I'm playing with Rails, and using sqlite3 database.

600 rows of data, one of the columns is populated with different strings of text, but all upper case.

What options are there for changing the uppercase data to lower case for all rows in that column ?

Presumably I could run some SQL on SQL command line, but is there a neat 'rails' way to do it ?


Solution

  • Try something like that in your console:

    ActiveRecord::Base.connection.execute "UPDATE table SET column = LOWER(column)"
    

    Another ways:

    # Validations & callbacks are skipped
    <Model>.all.map { |m| m.update_column :<column>, m.<column>.downcase }
    
    # Validations are skipped & callbacks are invoked
    <Model>.all.map { |m| m.update_attribute :<column>, m.<column>.downcase }