views:

39

answers:

1

I have seen some apps that have a few rake tasks included to load data. I am not talking about seed data, I know about db/seeds.rb, instead I am referring to data such as default users and basic records that help me fill my application with something to look at. I don't want to use db:fixtures:load because I don't have any control over this...

I would like to have rake tasks like this:

rake myapp:data:delete
rake myapp:data:load
rake myapp:data:reload

Where the 'delete' rake task would delete all data that I specify in the rake task, the 'load' app will load the default data from the task into the app and the 'reload' task will delete all data, then load it in the app. How do I do something like this?

If you could give me an example where I have a model named 'Contact' and a few fields - basically how to add or delete data from those fields in a rake task, I would REALLY appreciate it!

Just to give you an idea, I would mainly use these rake tasks when I move from one computer to another to do development. I don't want to manually go enter default records (such as my user to login with) so I could just do rake myapp:data:reload - this would be after doing rake db:schema:load

Thank you,

BN

+1  A: 

Create a file lib/tasks/data.rake and write the following code:

require File.join(File.dirname(__FILE__), '../../config/environment')
require 'database_cleaner'

namespace :myapp do
  namespace :data do

    task :delete do
      DatabaseCleaner.strategy = :truncation
      DatabaseCleaner.clean
    end

    task :load do
      require 'db/data.rb'
    end

    task :reload do
      Rake::Task['myapp:data:delete'].invoke
      Rake::Task['myapp:data:load'].invoke
    end

  end
end

So now you have defined your rake tasks. I'm using the gem database_cleaner, so you'll need to install it:

sudo gem install database_cleaner

Now, the rake myapp:data:load is basically loading the data from a file called db/data.rb. You could name it anything you wanted as long as you used the file name in the rake task or you could use more than one file if you wanted... So create the file db/data.rb and put all the code that you need...

User.create(...)
jordinl
Thanks for the prompt reply! I did exactly as you said, but for some reason it's not finding the db/data.rb file. Here's the error I get:
rake aborted!no such file to load -- db/data.rb(See full trace by running task with --trace)bash$
I have tried everything... It will not find ../db/data.rb, ../../db/data.rb or db/data.rb - I am having no luck. Please help. :(
That's weird, never happened to me :S The db folder where data.rb lives, is the same where you have the migrate folder, right? If so, maybe you could try: require File.join(File.dirname( _ _ FILE _ _ ), '../../db/data.rb') without the spaces between the underscores
jordinl
Ahh - I got it working. THank you! THe only problem I have now is that the data.rb file errors out when executed because User.create can't find 'User' in my model. Am I suppose to make the data.rb a class an inherit from activeREcord or something?
I figured out how to make it work - I had to pass the environment into the .rb file like this task :load => :environment do instead of just doing task :load do Thanks! by the way, how do I accept your answer as correct on SO?
Good stuff, quite weird the problems you've encountered... I never had them before. I don't know how to add best answer, never asked anything :P
jordinl