Mark Mapper

Installation in Rails

Using MarkMapper with Rails 4 is quite easy.

First, if you’re generating a new Rails application, it is recommended to leave out the ActiveRecord dependencies (unless you need them of course). From the console, just run:

rails new my_app --skip-active-record

But, not everyone is starting fresh. If you’re not upgrading, but just converting an existing Rails application from ActiveRecord (or another ORM), simply open config/application.rb and replace:

require 'rails/all'

With Rails 4:

# Pick the frameworks you want:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_model/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "rails/test_unit/railtie"

Next, add MarkMapper to your Gemfile, and run bundle install:

gem 'mark_mapper'

Now, you’re almost ready to go, but you still need some configuration info. Generate config/marklogic.yml and config/initializers/markmapper.rb by running:

bundle exec rails generate mark_mapper:config

To create your application inside of MarkLogic you need to run the rake task.

rake app:create

You might also like to configure Rails’ model generator to create MarkMapper models. Inside of the Application class (config/application.rb) add:

config.generators do |g|
  g.orm :mark_mapper
end

One other small note, make sure any ActiveRecord related configuration items are commented out or removed like below:

# config.active_record.whitelist_attributes = true

This will allow you to use the rails generate model command with MarkMapper.

You’re now finished, go forth and create!

Generate a user model with bundle exec rails g model user name:string:

invoke  mark_mapper
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

Create a user with bundle exec rails c:

Loading development environment (Rails 4.1.1)
irb(main):001:0> user = User.new(name: 'MarkLogic')
=> #<User _id: MarkMapper::ObjectId('539645eb43ebd927b2000001'), name: "MarkLogic">
irb(main):002:0> user.valid?
=> true
irb(main):003:0> user.save
=> true
irb(main):004:0> User.all.count
=> 1
Fork me on GitHub //