ActiveMongo is a Rails 3.0 and Ruby 1.9 compatible Mongo ORM.
It currently supports all common validations, find (single or many items) and has_many association.
Feel free to it if you’re adventurous.
- Add to your Rails application Gemfile
- Run gem bundle
- Load gem. For example, create initializer that says “require ‘active_mongo’”
- Create config/mongo.yml. Format is the same as database.yml. Database, host and port are required. User and password are optional
- Create a model, which is a subclass of ActiveMongo::Base
All ActiveModel validations are supported. validates_uniqueness_of with optional :scope => [:column] is available too.
Model.find(ID) # get by ID
Model.find #get whole collection
Model.find(args) #run a query, args are the same as for mongo_ruby_driver
Model.destroy_all
Model.destroy_all(args)
Model.new()
Model.new(:attribute => "value")
Model.save
object.attribute = "value" #No need to declare anywhere before setting, see?
object.update_attributes(:attribute => "value") #does not save!
object.unset(:attribute) #removes :attribute from the object and saves ONLY this change
object.save
object.save(false) #do not run validations
object.destroy
#in model
has_many :items#, :class_name => "Item", :foreign_key => "item_id"
#in code
object.items.find()
object.items.remove_all()
object.items.new()
You can limit what may be assigned by passing a hash to #new or #update_attributes with this option.
attr_accessible :attribute
attr_clear :attribute
ensure_index :attribute
ensure_index :attribute, :unique => true
ensure_index [ [:attribute, Mongo::ASCENDING] ], :unique => true # takes mongo-driver syntax
after initialize and before/after/around create, update and save callbacks are supported
after_save :method
#in model
named_scope :with_value, :attribute => :value #all mongo_driver find() parameters accepted
#in code
Model.with_value #returns scoped class
Model.with_value.find() #run find with scope
Model.with_value.new() #initialize an object with scope
- Documentation
- Tests
- Even more goodies
This code is distributed under MIT license.