Friday, March 27, 2009

Duplicating Activerecord record

Was trying to copy an existing DB record by using
parent_record = User.find(1)

new_user = = 'new_name'

But this ended in throwing stringify_keys error for User

This can done by specifying

new_user =

Hope this helps!

Wednesday, March 25, 2009

Binary conversion in Ruby

We hardly stretch ourselves to limit unless situation demands to do so.

My requirement - convert a decimal to binary conversion

My first impression - there would be a to_b default method available in ruby which I can use straight away. But it was not so.

But as always it is quite simple to do it in ruby,


gives the binary equivalent of the number

try this from ur IRB


similarly for any base conversion just pass the base value to to_s


3.to_s(10) for base 10 conversion.

Monday, March 16, 2009

Database log file size issue

Recently I faced a problem where my SQLServer 2005 ran out of space for log files. Usually the log files can be reduced by shrinking them but this time the transaction logs were so much huge that I was not even allowed to run shrink log command.

My DB was in Recovery phase.

My friend Dagupati ( a professional DBA ) helped me getting out of this trouble.

Following are the steps to be followed in sequence to achieve this.

1) Take the DB into emergency mode.

2) Bring DB to single user mode.


3)Repair you DB


if the above command fails, try this


NOTE : This may result in dataloss

4) Bring you DB to multi user mode


By now you should have your DB up & running.

DB at times enter into SUSPECT mode - restarting server should solve them if not follow the above steps.

To be on safer side take full / transactional backups regularly

Handel database NULL values in rails

I was in the impression that rails takes care of

1) nil to NULL when inserting, and

2) NULL to nil while fetching records through Active record

But this proved wrong when my application running in SQL server 2005 fails to detect database NULL fetched from DB.

The fetched value was not even caught by blank? method.

Found this article while googling on this issue, this more or less matched the problem I was facing.

I tried it and was successful.

I am providing a snapshot of the article
ActiveRecord::Base.class_eval do
def attributes_from_column_definition_with_null_fix
returning attributes = attributes_from_column_definition_without_null_fix do
attributes.each_value { |value| value.replace('') if value == 'NULL' }

alias_method_chain :attributes_from_column_definition, :null_fix

This helped me in dealing with DB NULL values. Certainly without this article it would have been a tough going to find a way around for this issue ;).

Have also made a thread for this in WorkingWithRails, just to make sure the fix is valid and to get expert comments on the fix.

Sunday, March 15, 2009

Calling RAKE tasks

The syntax for calling a Rake task within rails env,


If you need to call a rake task from a standalone ruby script, then do remember to include 'rake' files.

Bellow I have provided an usage on how to call a RAKE task from a rufus scheduler script.

My scheduler script is placed inside /lib folder

Also the script will fail to locate your rake task if just call a RAKE method without loading the RAKE file ( the .rake file in which you have your tasks defined ).

So, make sure you load your rake task files.

require 'rubygems'
require 'rake'
require 'rufus/scheduler'

load File.join( RAILS_ROOT, 'lib',
'tasks', 'my_task.rake')

scheduler = Rufus::Scheduler.start_new

scheduler.cron("0 17 * * 0") do

This when triggered will call 'delete_order_files' task defined in a .rake file called MY_TASK.RAKE having namespace 'MAINTENANCE'

Monday, March 9, 2009

Schedule using Rufus scheduler

We learned how to create Rake tasks in rails app. Next thing in process will be scheduling these tasks.

I opted for Rufu-Scheduler, mainly bcs it is quite simple to implement/configure

You can download the gem directly from :

There some good resources in net which describes usage of Rufus scheduler.

Like :

But some how I got myself into trouble in getting this to working ;(

Following are questions that bubbled in my mind while implementing Rufus.

>> Where do I put the scheduler code?

>> How will rails trigger my tasks, do I need to include something in my config. file?

I created a file in my /lib dir named myscheduler.rb, this contains the scheduler code and inorder to allow rails to load your rufus scheduler, you have to do

require ' myscheduler'

in environment.rb

My myscheduler.rb has

require 'rubygems'
require 'rake'
require 'rufus/scheduler'
load File.join( RAILS_ROOT, 'lib',
'tasks', 'mytasks.rake')

# Note include file containing your rake tasks
# Else you will get - Don’t know how to build
#task XXXXX’ error for the tasks you
#have defined

scheduler = Rufus::Scheduler.start_new

scheduler.every("1m") do
puts "Scheduler invoked"

restart the server and check for 'Scheduler invoked' in your CMD.

Got it ......... your half done with your scheduling, take a break ;)