public
Description:
Homepage:
Clone URL: git://github.com/mongodb/mongo-activerecord-ruby.git
mdirolf (author)
Mon Jun 15 07:34:41 -0700 2009
commit  508657d5cbe7520ed7db3538877054aa6ecf9da6
tree    e9a16d39b3025aee5498b3a776cc9230d9ace99e
parent  a7e9f71737143cbaf8a0864b9437a92cfbf0578e
name age message
file .gitignore Fri Jan 16 14:30:32 -0800 2009 create/fix gem:install task [Jim Menard]
file README.rdoc Loading commit data...
file Rakefile Fri Jan 16 14:30:32 -0800 2009 create/fix gem:install task [Jim Menard]
directory examples/ Thu Jan 29 07:48:13 -0800 2009 Honor MONGO_RUBY_DRIVER_{HOST,PORT} env vars in... [Jim Menard]
directory lib/
file mongo-activerecord-ruby.gemspec
directory tests/
README.rdoc

Welcome to MongoRecord

MongoRecord is an ActiveRecord-like framework for the 10gen Mongo database.

This library is for use outside of Ruby on Rails. If you want to use Mongo with Ruby on Rails, please see the ‘activerecord-mongo-adapter’ project at github.com/mongodb/activerecord-mongo-adapter.

This document assumes you have read the Mongo documentation.

A quick code sample:

  require 'rubygems'
  require 'mongo'
  require 'mongo_record'

  class Track < MongoRecord::Base
    collection_name :tracks
    fields :artist, :album, :song, :track
    index :artist

    def to_s
      "artist: #{artist}, album: #{album}, song: #@song, track: #{@track ? @track.to_i : nil}"
    end
  end

  MongoRecord::Base.connection =
    XGen::Mongo::Driver::Mongo.new.db('mongorecord-test')

  t = Track.new(:artist => 'Level 42', :album => 'Standing In The Light',
                :song => 'Micro-Kid', :track => 1)
  t.save
  puts "There are #{Track.count()} tracks."
  t = Track.find(:first, :conditions => {:song => 'Micro-Kid'})
  Track.find(:all, :sort => 'song').each { |t| puts t.to_s }

Installation

  $ gem sources -a http://gems.github.com
  $ sudo gem install mongodb-mongo_record

MongoRecord depends on the Mongo Ruby Driver, version 0.5.4 or higher. Installing the MongoRecord gem will also install the Mongo Ruby Driver if you don’t have it already.

The source code is available at github.com/mongodb/mongo-ruby-driver. You can either clone the git repository or download a tarball or zip file. Once you have the source, you can use it from wherever you downloaded it or you can install it as a gem from the source by typing

  $ rake gem:install

Note: when you install the gem this way it is called "mongo_record", not "mongodb-mongo_record". In either case, you "require ‘mongo_record’" in your source code.

Getting Started

See the examples, read the MongoRecord::Base and MongoRecord::Cursor documentation, and look at tests/test_mongo.rb.

Persistence

You can use MongoRecord::Base or talk to the database (stored in the $db object) directly.

See MongoRecord::Base and MongoRecord::Cursor.

Logger

See MongoRecord::LogDevice. When running outside of the cloud (for example, during development), all log messages are echoed to $stderr which is normally the console.

Credits

Jim Mulholland, jim at squeejee dot com

  • Ability to save custom attributes not declared in the model
  • Save and retrieve custom "_id" fields
  • Find_each functionality
  • Find_last based on the created_at field
  • Assigning created_at and updated_at fields even if they are not declared
  • Alias methods for "first", "last" and "all"

Clinton R. Nixon, crnixon at gmail dot com

  • Ability to define and query indexes from models