Commonly Used Active Record Operations on Rails

Migrations within Rails

To generate a new migration, use
$ rails generate migration MyNewMigration
where MyNewMigration is the name of your migration. An empty migration file timestamp_my_new_migration.rb will be created in the db/migrate/ directory. You can modify that file with commands e.g. create_table, add_column.

class AddSystemSettings < ActiveRecord::Migration
def up
  create_table :system_settings do |t|
   t.string :name
   t.string :label
   t.text :value
   t.string :type
   t.integer :position

SystemSetting.create name: ‘notice’,
   label: ‘Use notice?’,
   value: 1

def down
   drop_table :system_settings

If you use a more special syntactic command,
$ rails generate migration add_fieldname_to_tablename fieldname:string
a non-empty file timestamp_add_fieldname_to_tablename will be generated, with some default operations in it:

class AddFieldnameToTablename < ActiveRecord::Migration
   def change
       add_column :tablenames, :field, :string

remove_fieldname_from_tablename is also helpful. Then use
$ rake db:migrate
to update the database. You can roll back with parameters “STEP=x“, where x is the version to which you want to downgrade.

Available Transform [1]

  • create_table(name, options): Creates a table called name and makes the table object available to a block that can then add columns to it, following the same format as add_column. See example above. The options hash is for fragments like “DEFAULT CHARSET=UTF-8” that are appended to the create table definition.
  • drop_table(name): Drops the table called name.
  • change_table(name, options): Allows to make column alterations to the table called name. It makes the table object available to a block that can then add/remove columns, indexes or foreign keys to it.
  • rename_table(old_name, new_name): Renames the table called old_name to new_name.
  • add_column(table_name, column_name, type, options): Adds a new column to the table called table_name named column_name specified to be one of the following types: :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean. A default value can be specified by passing an options hash like { default: 11 }. Other options include :limit and :null (e.g. { limit: 50, null: false }).
  • rename_column(table_name, column_name, new_column_name): Renames a column but keeps the type and content.
  • change_column(table_name, column_name, type, options): Changes the column to a different type using the same parameters as add_column.
  • remove_column(table_name, column_name, type, options): Removes the column named column_name from the table called table_name.
  • add_index(table_name, column_names, options): Adds a new index with the name of the column. Other options include :name, :unique (e.g. { name: ‘users_name_index’, unique: true }) and :order (e.g. { order: { name: :desc } }).
  • remove_index(table_name, column: column_name): Removes the index specified by column_name.
  • remove_index(table_name, name: index_name): Removes the index specified by index_name.

Active Record Query

AR Query is also available both in Migration files and Rails console. It can be used conveniently to request the values stored in the database or update some values without writing a Migration file.

Here are some examples:

client = Client.find(10)
# => #
client = Client.first
# => #
client = Client.last
# => #
Client.find_by first_name: ‘Lifo’
# => #

Then you can simply modify the variables with
client.first_name = “Yuhc”

Update the database with

For more details, please reference to the following articles or documents.


[1] Active Record Migrations,
[2] Active Record Migrations,
[3] Active Record Queries (Chinese),

Leave a Reply

Time limit is exhausted. Please reload CAPTCHA.