Tuesday, August 21, 2012

Rails daily usage cheat sheet


Task Command
Start server rails s
Start console rails c
Generate a db migration to add columns
rails generate migration add_column_to_some_table column_name:string...
  • class AddColumnBankAccountNameToRequests < ActiveRecord::Migration
    def self.up
    add_column :direct_debit_requests, :bank_account_name, :string
    end
    def self.down
    remove_column :direct_debit_requests, :bank_account_name
    end
    end
Generate a db migration to add index
rails generate migration add_index_to_some_table
  • class AddIndexToActionAudits < ActiveRecord::Migration
    def self.up
    add_index :action_audits, [:controller, :action, :action_failed]
    end
    def self.down
    remove_index :action_audits, [:controller, :action, :action_failed]
    end
    end
Generate new model
  • class CreateLoanCharges < ActiveRecord::Migration
    def self.up
    create_table :loan_charges do |t|
    t.references :product
    t.string :listener_reference
    t.integer :fixed_charge
    t.integer :min_charge
    t.integer :max_charge
    t.float :percentage_change
    t.timestamps
    end
    end
    def self.down
    drop_table :loan_charges
    end
    end

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.