Using Datamapper with existing rails application
Asked Answered
A

1

1

I have an existing Rails 3 application using ActiveRecord, and I want to switch to Datamapper. The instructions given in the dm-rails page only talk about creating a new application. Does anyone know how to throw away all activerecord dependancies and migrate to datamapper?

Thanks!

Annuity answered 15/12, 2011 at 22:21 Comment(2)
just out of curiosity, why would you want to replace AR with DM?Polymath
@jpartogi, probably because the application is built around an existing database schema. AR is a complete PITA if your schema isn't the way AR wants it. We're in the same boat.Batwing
B
2

It's realtively straightforward, but there are a couple of things you need to do.

In your Gemfile, remove "rails" and instead require the following.

gem 'activesupport',      RAILS_VERSION, :require => 'active_support'
gem 'actionpack',         RAILS_VERSION, :require => 'action_pack'
gem 'actionmailer',       RAILS_VERSION, :require => 'action_mailer'
gem 'railties',           RAILS_VERSION, :require => 'rails'

Where RAILS_VERSION is the version of Rails you want to use (e.g. ~> 3.1). This is basically all of rails except ActiveRecord.

At the top of config/application.rb, remove the require for rails (I forget what the original require looks like) and replace it with specific requires for the railties you need.

require "action_controller/railtie"
require "action_mailer/railtie"

I think the only other one is a Test::Unit railtie, but we're not using Test::Unit, so we don't include it.

Finally, if you want to use the identity map (I suggest you do, but it's not needed), place in your ApplicationController's class body:

use Rails::DataMapper::Middleware::IdentityMap

That should be everything; the rest is just configuring your database.yml according to the README (it's pretty much cross-compatible with a standard rails one anyway).

For reference, take a look at what the generators does:

-zsh$  curl http://datamapper.org/templates/rails.rb
apply 'http://datamapper.org/templates/rails/gemfile.rb'
apply 'http://datamapper.org/templates/rails/application.rb'

If you look at the contents of those two files you'll see the extra stuff you'd get if you had used the generator.

Batwing answered 16/12, 2011 at 0:26 Comment(1)
Thanks! Your instructions worked. The links to the file were invaluable!Annuity

© 2022 - 2024 — McMap. All rights reserved.