Ruby on Rails
HowToUseRailsWithoutADatabase

This is an OpenQuestion for me, but perhaps someone else knows…

Rails itself does not require a database. You are free to substitute anything else instead of ActiveRecord for your model class (ActiveRecord is heavily wed to SQL).

If by “without a database” you mean “without a database server (daemon)”, then take a look at SQLite. It doesn’t run as a separate server and provides SQL.

Potential Non-database models

Examples

Checkout Instiki 0.10.0 from SVN repository at dev.instiki.org and see how Madeleine is used instead of ActiveRecord. Particularly, have a look at lib/active_record_stub.rb.

Keeping Rails from establishing a DB connection

Rails 1.0

In config/environment.rb, inside the Rails::Initializer block, add this line:


config.frameworks -= [ :active_record ]

To keep Test::Unit from giving you fixture related errors update test/test_helper.rb like so:


class Test::Unit::TestCase
  self.use_transactional_fixtures = false
  self.use_instantiated_fixtures  = false
  def load_fixtures
  end
end

Rails 0.12.1

Commented out the following line from config/environment.rb:


# ActiveRecord::Base.establish_connection

Change the test tasks

Rails 1.1.2

Create a file in lib/tasks called testing.rake (or anything else, as long as it has a .rake extension) and add the following to it:


[:'test:units', :'test:functionals', :'test:recent', :'test:uncommitted', :'test:integration'].each do |name|
  Rake::Task[name].prerequisites.clear
end

Thanks to http://jayfields.blogspot.com/2006/06/ruby-on-rails-unit-tests.html and Rails Recipes for this.

Rails 1.0

Create a file in lib/tasks called testing.rake. Add the following lines to it:


def undefine(*names)
  names.each do |name|
    app = Rake.application
    tasks = app.instance_variable_get('@tasks')
    tasks.delete(name)
  end
end

undefine "prepare_database" 
desc "Dummy task because we no longer need to prepare the database while running tests" 
task :prepare_database do
end

Rails 0.12.1

Remove the dependancies on :prepare_test_database from the test tasks in the Rakefile.

Use a non-database “virtual” table

Let’s say you have an existing model with lots of validations, but you suddenly decide that you want to delete the table and store the data as a field in an existing table. (For effeciency, expandability, etc.)


class BaseRecord
     def save; end
     def update_attribute; end
     def new_record?; end
     include ActiveRecord::Validations

     def [](key)
       instance_variable_get(key)
     end

     def BaseRecord.human_attribute_name(attribute_key_name)
        attribute_key_name.to_s
     end

    def attributes=(attributes)
        return if attributes.nil?
        attributes.stringify_keys!
        multi_parameter_attributes = []
# (if you need multi parameter attributes, write code here)
#        remove_attributes_protected_from_mass_assignment(
        attributes.each do |k, v|
#          k.include?("(") ? multi_parameter_attributes << [ k, v ] : send(k + "=", v)
          send(k + "=", v)
        end
#        assign_multiparameter_attributes(multi_parameter_attributes)
      end
end
class MyThings < BaseRecord

 attr_accessor :this_thing, :that_thing

#.. existing validations, etc.
end

Now you can do

x = \MyThings.new; x.attributes = params[:mythings]
just like before. Lastly, save the data with
require 'yaml'; somefield = \MyThings.to_yaml 

Special thanks to John Long (i.e. blatently copied from here )