Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What are Rake tasks ?

    • 0
    • 2
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 437
    Comment on it

    Rake is a tool that is written in Ruby language and very similar to Ant, Make. The difference between the other tools and rake is that it is a domain specific language which means outside the boundaries of rails it has no existence. It basically extends ruby so you can use all the extensions and features that come with the ruby language. It is really a powerful tool once you will use it you will come to know after that. We can write our own custom tasks like reading emails, reindexing the indexes of solr etc. You can create tasks with same name but in different namespaces. Namespaces are used to avoid collisions.

    For example, you can create two tasks with the name populate.

    employee:populate  This task populates up the data of the employees.
    products:populate  This task populates up the data of the products.
    

    Example of a complete rake task where we have a namespace and tasks

    namespace :rebuild do
      desc "Rebuild the structure of first table"
      task :a => :environment do
        puts "a: #{rebuild(User).name}"
      end
    
      desc "Rebuild the structure of second table"
      task :b => :environment do
        puts "b: #{rebuild(User).name}"
      end
    
      desc "Rebuild a and b"
      task :all => [:a, :b]
    
      def rebuild(model_class)
        model_class.find(:first, :order => 'RAND()')
      end
    end
    

    we can also set tasks to call by default

    task :default => :callme_first
    

    Apart from that we can have tasks which will accepts arguments:

    task :create, [:arg1, :arg2] do |t, args|
      puts "First argument: #{args[:arg1]}"
      puts "Second argument: #{args[:arg2]}"
    end
    
    rake create[1,random_string]
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: