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
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.
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
Commented out the following line from config/environment.rb:
# ActiveRecord::Base.establish_connection
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
Remove the dependancies on :prepare_test_database from the test tasks in the Rakefile.
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 )
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
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.
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
Commented out the following line from config/environment.rb:
# ActiveRecord::Base.establish_connection
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
Remove the dependancies on :prepare_test_database from the test tasks in the Rakefile.
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 )