Using Datamapper with existing rails application

牧云@^-^@ 提交于 2019-12-08 07:24:26

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/8527459/using-datamapper-with-existing-rails-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!