Monday, March 16, 2009

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' }
end
end

alias_method_chain :attributes_from_column_definition, :null_fix
end

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,

Rake::Task[TASK_NAME].invoke

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.

USAGE
 
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
Rake::Task["maintenance:delete_order_files"].invoke
end

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 : http://gems.rubyforge.vm.bytemark.co.uk/gems/

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

Like :http://rufus.rubyforge.org/rufus-scheduler/

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"
end

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

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

Wednesday, February 18, 2009

Raking in rails

Rake tasks are one of the most power but still under utilized part in rails.

Building rake tasks makes in much easier for manual execution of scripts and plays a big role when scheduling a job.

We might have taken the command 'rake db:migrate' lightly, but it is the time to explore the components behind this.

'Rake --tasks ' lists the available tasks that can be performed by rake

We can create our now Rake tasks by creating .rake files in side lib/tasks dir in rails app.

Introducing NAMESPACE and TASK

TASK : This block defines what actually you want your ruby code to perform.
 
task :my_first_rake_task do

puts "This is my first rake code"

end

save this sample code in lib/tasks with any name ( with rake ext. )

Now, in cmd run 'rake my_first_rake_task'

...... existed!?? theres still more......

NAMESPACE

To manage your tasks in a better/ logical way you can bind them to a NAMESPACE

USAGE
 
namespace :test do
task :my_first_rake_task do

puts "This is my first rake code"

end
end


in CMD run rake test:my_first_rake_task

Description

Now that you have learned how to create a rake task, Next question is ........ how to give description for the task you have created????

simple...

use desc
 
namespace :test do
desc "This is a simple example to show the usage of rake tasks in rails"
task :my_first_rake_task do

puts "This is my first rake code"

end
end

In CMD type RAKE --tasks

one of the entry in the list will be

rake test:my_first_rake_task #This is a simple example to show the usage of rake tasks in rails

Wednesday, January 28, 2009

Ungenerate Stuffs in rails

Rails 'generators' makes it quite easy to create basic model, controller, helper & test skeleton files

But lately I found that it is equally easy to clear files created by generator by using DESTROY.

Example

ruby script/destroy controller XXXX

Will remove all files dumped by rails for command ruby script/generate controller XXXX

ruby script/destroy scaffold XXXX

Will remove all files dumped by rails for command ruby script/generate scaffold XXXX