Radio 21 Gram

Radio21g.com official blog

Archive for the ‘rails’ Category

Jul
10

Rails Migration: enable rollback

Posted by roosevelt under rails

With Rails Migration, you can easily add or update your DB schema with a command “rake db:migrate“.

But if you just realize that you gave the wrong datatype for your new column or you want to rename your new column, you may want to rollback to your previous schema version and re-run the update again. To rollback to your previous schema, you need issue the command:

    # rake db:migrate VERSION=prev_version

where prev_version is the last version number of your DB schema, you need to find it out either by checking your last db migration files name or check the SCHEMA table in your DB. This apparently is not very convinient.

To be a little smarter, try putting the following code into a file called <RAILS_HOME>/lib/tasks/rollback.task, this will let you achieve the above by just issuing a single command like:

    # rake db:rollback

additionally, to check the current DB schema version:

    # rake db:current

namespace :db do
  desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
  task :rollback => :environment do
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    version = ActiveRecord::Migrator.current_version - step
    ActiveRecord::Migrator.migrate('db/migrate/', version)
  end
end

namespace :db do
  desc 'get current schema version'
  task :current => :environment do
    puts ActiveRecord::Migrator.current_version
  end
end
May
05

Use Unicode in Ruby on Rails Application

Posted by roosevelt under rails, mysql

When creating a new web application, it’s a good practice to use Unicode encoding in your database even when your content are all in English. Radio21g.com is created with UTF-8 from day 1 with following 2 steps:

  1. To make sure your database is using Unicode to store the text data, make sure you create the database with encoding specified:
    create database radio21g character set utf8;
  2. After the database ready, you also need to tell the rails Mysql adapter to save and retrieve the data as unicode. You do this in the last line of the /config/database.yml:
development:
  adapter: mysql
  database: database
  username: root
  password:
  host: localhost
  encoding: utf8

In addition to database related settings, your web page (RHTML) should also display the text and submit the user input in UTF-8. To achieve that, you need to:

  1. Specified the “charset=UTF-8″ in your HTML head:
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  2. Set the Content-Type header in the rails @headers object of each request. Usually this is done in the /app/controller/applicaiton.rb with a filter:
        class ApplicationController < ActionController::Base end
            before_filter :set_charset
    
            def set_charset
                @headers["Content-Type"] = "text/html; charset=utf-8"
            end
    
        end

    For Rails 2.0 and above, this step is not needed anymore as UTF-8 is the default setting in the @headers.