Using Rails without a database

Today I was fiddling with Rails to create a simple web UI on top of some REST services. Therefor I didn’t really need (or is it really didn’t need) a database server. Just writing and running Rails code without a database isn’t a problem. Running unittests (and rspec tests) however is a problem. By default the unittest helpers used by the generated tests try to initialize a database connection.

Rails Recipes to the rescue. Recipe 14 (‘Rails without a database’) explains how to modify the helpers and environment configuration to get everything to work without a database and ActiveRecord. Kudos for pointing this out to Levi!!.

Getting the tests to work
open /test/test_helper.rb and replace it’s content with

[ruby]
ENV["RAILS_ENV"] = “test”
require File.expand_path(File.dirname(__FILE__) + “/../config/environment”)
require ‘application’
require ‘test/unit’
require ‘action_controller/test_process’
require ‘action_web_service/test_invoke’
require ‘breakpoint’
[/ruby]

Getting the tests to work with Rake
In this case the solution proposed in Rails Recipes actually didn’t work; it referenced a non-existing ‘lookup’ method on the Rake::Task class. That wasn’t to hard to solve.
In /lib/tasks/ create a file called ‘clear_database_prerequisites.rake’ and insert the following code:

[ruby]
[:test_units, :test_functional, :recent].each do |name|
Rake::Task[name].prerequisites.clear # I replaced ‘Task.lookup(name)’ by Task[name]
end
[/ruby]

According to Rails Recipes this single line looks up each Task using Rake’s API and clears the Task’s dependencies. Now according to Rails Recipes running ‘rake test’ should work as well; in my case it didn’t I had to disable loading ActiveRecord.

Disabling ActiveRecord altogether
It is possible to disable loading ActiveRecord by making a simple modification to ‘/config/environment.rb’:

[ruby]
# Skip frameworks you ‘ re not going to use
config.frameworks -= [ :active_record ]
[/ruby]

This was a bit complexer then I’d hoped, but I’m glad to be able to write AND run tests again!

Removing database.yml
After doing all of the above I found the following ticket in Rails’ issuetracker: http://dev.rubyonrails.org/ticket/7868 which claims that the convention is to remove your database.yml after which Active Record assumes it’s inactive. I tried it, but it doesn’t seem to work like that:

[code]
bash>rake test
(in /Users/peter/Development/rails/tttt)
/opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb"
/opt/local/bin/ruby -Ilib:test "/opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/rake_test_loader.rb"
No such file or directory - /Users/peter/Development/rails/tttt/config/../config/database.yml
...
[/code]

but this might be related to my setup. I’ll stick with the Rails Recipes solutions for now.

This entry was posted in rails, ruby. Bookmark the permalink.

6 Responses to Using Rails without a database

  1. Pingback: Aplicação sem banco de dados : Ruby Brasil

  2. Sazima says:

    Hey!

    Actually, all you need to do is edit environment.rb:

    # Skip frameworks you’re not going to use. To use Rails without a database
    # you must remove the Active Record framework.
    # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
    config.frameworks -= [ :active_record ]

    Cheers, Sazima

  3. Celso Dantas says:

    It helped my a lot! thanks!

  4. naveent says:

    thankyou sazima , u r rite.

  5. Arivarasan says:

    hi Sazima,

    I removed the comment for the line
    config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
    config.frameworks -= [ :active_record ]
    in the environment.rb. But it still doesn’t work!

  6. zman says:

    did you restart it after you removed the comments?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>