Pages

Friday, January 3, 2014

Rename a Database column using Rails migration

You need to create a separate migration to do this.

rails g migration FixColumnName

class FixColumnName < ActiveRecord::Migration
  def change
    rename_column :table_name, :old_column, :new_column
  end
end

If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again.
rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...
You could use change_table to keep things a little neater.
class FixColumnNames < ActiveRecord::Migration
  def change
    change_table :table_name do |t|
      t.rename :old_column1, :new_column1
      t.rename :old_column2, :new_column2
      ...
    end
  end
end

Thursday, January 2, 2014

Understanding attr_accessible – Secure Your Rails Application

What is the attr_accessible directive ?
So, when you open up your devise User model, and you notice a line similar to that :
attr_accessible :username, :email, :password
The first thing that is important to know about, if you don’t really, is that attr_accessible is simply an instance method, a function as most non-ruby programmers would name. This is actually a part of ActiveModel::MassAssignmentSecurity::ClassMethods, which already some something about its usage. It’s there to prevent mass assignment security attacks.
But what exactly is that ?
 In Ruby on Rails, a mass assignment is when you save more than one attributes of a model to your database. For instance :
current_user.save
current_user.update_attributes(...)
current_user.update_attribute(...)   <- THIS IS NOT A MASS ASSIGNMENT

Changes to be made after you Change your Repository URL

Change repository URL


You simply need to remove the origin remote that has the incorrect reference with:
$ git remote rm origin
Then restart the add, and push, and you should be good to go.
$ git remote add origin git@github.com:nikhil/example.git 
$ git push -u origin master

Capistrano deploy fails after the repository URL changes - How to fix?


You need to run following command before you start deploying with your new URL.
cap deploy:cleanup -s keep_releases=0
Apparently you will also need to remove shared/cached-copy, because this doesn't cleaned by the capistrano call above.