# Rake task organization

**URL:** <https://www.ruby-forum.com/t/rake-task-organization/175837>\
**Category:** Ruby\
**Created:** [September 21, 2009, 6:32am UTC](https://www.ruby-forum.com/t/rake-task-organization/175837 "2009-09-21T06:32:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [September 21, 2009, 6:32am UTC](https://www.ruby-forum.com/t/rake-task-organization/175837/1 "2009-09-21T06:32:06Z")

</div>

in the “connects” task for the following rakefile, how do I tie-in Item  
and Items so that there’s minimal code repetition? For instance,  
currently Item is defined in the connect task for the rakefile, wheras I  
would rather that Item be “imported” either by requiring a module or  
including the Item.rb file, for example.

What’s a good approach?

thufir@ARRAKIS:~/rb$  
thufir@ARRAKIS:~/rb$ cat Item.rb  
require ‘rubygems’  
require ‘active\_record’

class Item \< ActiveRecord::Base  
end  
thufir@ARRAKIS:~/rb$  
thufir@ARRAKIS:~/rb$ cat Items.rb  
class Items \< ActiveRecord::Migration  
def self.up  
create\_table :items do |t|  
t.column :title, :string  
t.column :content, :string  
t.column :source, :string  
t.column :url, :string  
t.column :timestamp, :timestamp  
t.column :keyword\_id, :integer  
t.column :guid, :string  
t.column :html, :string  
end  
end  
def self.down  
drop\_table :items  
end  
end

thufir@ARRAKIS:~/rb$  
thufir@ARRAKIS:~/rb$ cat rakefile.rb  
require ‘rubygems’  
require ‘active\_record’  
require ‘feed\_tools’  
require ‘yaml’

desc “connects to db”  
task :connect do  
db = YAML.load\_file(“database.yml”)  
ActiveRecord::Base.establish\_connection(  
:adapter =\> db[“adapter”],  
:host =\> db[“host”],  
:username =\> db[“username”],  
:password =\> db[“password”],  
:database =\> db[“database”])  
class Item \< ActiveRecord::Base ###take out this line?  
end  
puts “connected to db”  
end

desc “drop table items”  
task :drop =\> :connect do  
#drop table items  
puts “unable to drop table”  
end

desc “creates table items unless exists”  
task :create =\> :connect do  
#create table items  
puts “unable to create table”  
end

desc “populate db”  
task :populate =\> [:connect, :create] do  
puts “get data”  
end

desc “clobber the db”  
task :clobber =\> [:connect, :drop, :create] do  
puts “clobber it!”  
end  
thufir@ARRAKIS:~/rb$  
thufir@ARRAKIS:~/rb$

thanks,

Thufir
