GitHub – Delete a commit


If you are on that commit,

git reset --hard HEAD~1

Otherwise,

git reset --hard <sha1-commit-id>

If the changes were already pushed, then we have to do a force push to make it done.

git push origin HEAD --force

GitHub – Changing current branch to master


If you have a repository in Git, made a branch say new_branch and did some changes in both master and the new_branch.

After many commits when you are in a need to convert the new_branch to master and to discard the commits done on master, try with the following set of commands.

git checkout new_branch
git merge --strategy=ours master   # it just records a commit, but retains the contents of new_branch
git checkout master
git merge new_branch   # fast-forward master up to the merge

add_column and Postgresql – Rails migration


Just ran into a difference between MySQL and Postgresql when running a migration that added a new column to an existing table.

The intent of the original line was to add a new boolean column called ‘active‘ and set the default value to true:

add_column :users, :active, :boolean, :default => true

This works fine on MySQL but with Postgresql the default value was not used on existing records. It appears that Postgresql sees that NULL is an acceptable value for the column and uses that as the value.

The way to fix this is to add a ‘null‘ attribute to the call like this:

add_column :users, :active, :boolean, :default => true, :null => false

Now, running rake db:migrate sets all existing records to true

Update multiple rows in MySQL – rails


As we know multiple rows can be inserted in to the database using a single MySQL INSERT query. The syntax is,

sql = ActiveRecord::Base.connection(); /*Setting up ActiveRecord connection */
sql.execute("INSERT into products(name,description,vategory,user_id) values ('name1','desc1','categ1',1),('name2','desc2','categ2',2),('name3','desc3','categ3',3);")

On the other hand, update on multiple rows will be complex using UPDATE query. This could be achieved using INSERT along with ON DUPLICATE KEY UPDATE. The syntax is,

sql.execute("INSERT into products (id,name,description) VALUES (id-1,'name4','desc4'),(id-2,'name5','desc5'),(id-3,'name6','desc7') ON DUPLICATE KEY UPDATE name=VALUES(name),description=VALUES(description);")

escape_javascript – Rails


When there is a need to render partial files inside the JS code the escape_javascript can be used.
Example:

<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#some_div').innerHTML="<%=escape_javascript(render :partial=>'some/partial',:locals=>{:data=>true})%>";
});</script>

Secure the password(sensitive) data from logging – Rails


The sensitive parameters can be secured from logging using ‘filter_parameter_logging’ for Rails 2.3.*.
i.e., the password or other sensitive parameters can be secured at the time of Sign-in or Sign-up by the following way:

class ApplicationController < ActionController::Base
---
filter_parameter_logging :password, "password_confirmation"
---
end

After this, the params for ‘password’ and ‘password_confirmation’ will be saved as “[FILTERED]” in log file.

Using ‘having’ in Rails query


To compare two fields of a table, we can use ‘having‘ in rails(2.3.*) query. I can be also used instead of ‘conditions‘, but ‘having‘ will work only along with ‘group‘ option.

The example query is as follows:

Compare two fields:

User.find(:all,:group=>'email',:having=>'created_at = updated_at')

Alternate for ‘conditions‘:

User.find(:all,:group=>'email',:having=>['age < ?',23])

Running Migrations from console – Rails


We can also run Migrations from the console, since migration file is a ruby file.

>> require "db/migrate/20110105063841_add_column_name_to_users.rb"
=> ["AddColumnNameToUsers"]
>> AddColumnNameToUsers.up

This will work fine.

Asset packaging with Heroku + Jammit


Heroku due to its nature(read-only file system) won’t allow us to write inside the public directory. This makes assert-packaging with jammit a complex one.

The read/write permission was only to the log or tmp directory.
In order to resolve this problem, the heroku-jammit plugin can be used.

Step-1:
Install the Jammit gem by,

$ sudo gem install jammit

Add the Gem to your config/environment.rb

config.gem "jammit"

If you use .gems file for installing gem in server, then add ‘jammit’ to your .gems file.

For Rails-2.3.*

Add the following line in your config/routes.rb

require 'jammit'
ActionController::Routing::Routes.draw do |map|
...
Jammit::Routes.draw(map)
...
end

For Rails-3 the route will be loaded automatically.

Jammit uses config/assets.yml configuration file.

embed_assets: on
javascripts:
workspace:
- public/javascripts/javascript_1.js
- public/javascripts/javascript_2.js
- public/javascripts/jquery/ui/minified/javascript_3.js
- public/javascripts/javascript_4.js
- public/javascripts/javascript_n.js
stylesheets:
workspace:
- public/stylesheets/style_1.css
- public/stylesheets/style_2.css
- public/stylesheets/style_3.css
- public/stylesheets/style_4.css
- public/stylesheets/style_n.css
common:
- public/stylesheets/style_1.css
- public/stylesheets/style_2.css
- public/stylesheets/style_n.css
global
- public/stylesheets/style_1.css
- public/stylesheets/style_2.css
- public/stylesheets/style_n.css

For Rails-3 make sure that you have the following line in your config/environment.rb, since by default the static asserts will not be served

config.serve_static_assets = true

Usage:

<%= include_stylesheets :common, :workspace, :media => 'all' %>
<%= include_javascripts :workspace %>

Step-2:

Add the heroku-jammit plugin to your Heroku by,

$ heroku plugins:install https://github.com/chebyte/heroku-jammit.git

Usage:
Whenever you change any javascript/css files configured in your asserts.yml file, you have to use any of the following tasks to make your changes get reflected.

$ heroku jammit:add

– this task will make the asserts and commit it.

$ heroku jammit:delete

– this task will remove asserts and commit it.

This will work fine.

Before saving an object record’s field value


Before saving a field value of the object record, the following options can be possible:

>>u=User.find_by_login("pradeep")
=> #(User id: 32, login: "pradeep", email: "p.pradeep@sedin.co.in", crypted_password: "f70c133388fc64d59c979efa00aff06d7f44ce20", salt: "059b6c5fcc86680ee5a453b33627075e553e0b0c", created_at: "2010-11-18 10:38:41", updated_at: "2010-11-20 12:50:55", remember_token: nil, remember_token_expires_at: nil, activation_code: nil, activated_at: "2010-11-18 11:17:28", state: "active", deleted_at: nil, admin: false, password_reset_code: nil, sent_activation: true)
>> u.admin
=> false
#initial value
>> u.admin=true
=> true
>> u.changed
=> ["admin"]
>> u.changed?
=> true
>> u.changes
=> {"admin"=>[false, true]}
>> u.admin_was
=> false
>> u.admin_change
=> [false, true]
>> u.admin_changed?
=> true
>> u.save
=> true