Skip to content

Instantly share code, notes, and snippets.

View brianhempel's full-sized avatar

Brian Hempel brianhempel

View GitHub Profile
@brianhempel
brianhempel / uninitialized constant ActiveModel::Naming
Created February 25, 2011 22:28
MongoMapper mmconsole broken in Ruby 1.8.7
$ bin/mmconsole
ruby-1.8.7-p299 > require 'rubygems' # or try to define a MM class
NameError: uninitialized constant ActiveModel::Naming
from ~/.rvm/gems/ruby-1.8.7-p299@mmtest/gems/activemodel-3.0.4/lib/active_model/translation.rb:24
from ./bin/../lib/mongo_mapper/translation.rb:4
from ~/.rvm/gems/ruby-1.8.7-p299@mmtest/gems/activesupport-3.0.4/lib/active_support/core_ext/module/introspection.rb:70:in `const_get'
from ~/.rvm/gems/ruby-1.8.7-p299@mmtest/gems/activesupport-3.0.4/lib/active_support/core_ext/module/introspection.rb:70:in `local_constants'
from ~/.rvm/gems/ruby-1.8.7-p299@mmtest/gems/activesupport-3.0.4/lib/active_support/core_ext/module/introspection.rb:70:in `each'
from ~/.rvm/gems/ruby-1.8.7-p299@mmtest/gems/activesupport-3.0.4/lib/active_support/core_ext/module/introspection.rb:70:in `local_constants'
from ~/.rvm/gems/ruby-1.8.7-p299@mmtest/gems/activesupport-3.0.4/lib/active_support/core_ext/module/introspection.rb:68:in `each'
@brianhempel
brianhempel / mm_indexes.rb
Created February 22, 2011 00:11
Indexing in MongoMapper
def build_indexes!
ensure_index [["parent_id", Mongo::ASCENDING]]
ensure_index [["place_type", Mongo::ASCENDING], ["name", Mongo::ASCENDING]]
ensure_index [["ancestor_ids", Mongo::ASCENDING], ["place_type", Mongo::ASCENDING]]
end
def ensure_index(index_keys)
puts "Ensuring index on #{index_keys.map{|i| i[0]}.join(', ')}..."
collection.ensure_index index_keys
end
@brianhempel
brianhempel / mongo_config.rb
Created February 21, 2011 23:53
Don't need much to get MM running
# ENV['MONGOHQ_URL'] = 'mongodb://localhost/sushi'
MongoMapper.database = "mes-#{Rails.env}"
# if ENV['MONGOHQ_URL']
# MongoMapper.config = {RAILS_ENV => {'uri' => ENV['MONGOHQ_URL']}}
# MongoMapper.connect(RAILS_ENV)
# end
if Rails.env == "development"
@brianhempel
brianhempel / global_stat.rb
Created January 21, 2011 18:44
mongomapper custom types
class GlobalStats
include MongoMapper::Document
key :attempts, Attempts
end
class Attempts
attr_accessor :total, :hours
def self.to_mongo(attempts)
{
@brianhempel
brianhempel / polymorphic_playing.rb
Created January 14, 2011 18:34
MongoMapper Polymorphism with Single Collection Inheritance (SCI)
class Commentable
include MongoMapper::Document
many :comments, :as => :commentable
end
class Article < Commentable
end
class Product < Commentable
end
class Person
include MongoMapper::Document
end
class Registree < Person
key :in_wels, Boolean
key :in_vienna, Boolean
end
class Visitor < Registree
class Person
include MongoMapper::Document
end
class Registree < Person
key :in_wels, Boolean
key :in_vienna, Boolean
end
class Visitor
class Person
include MongoMapper::Document
end
class Registree < Person
key :in_wels, Boolean
key :in_vienna, Boolean
end
class Visitor
# single collection inheritance
# all will be stored in the Person collection in the db
class Person
include MongoMapper::Document
end
class Registree < Person
key :visitor, Boolean
scope :vistors, :visitor => true
criteria.merge!( :_keywords => { :$in => words } )
search_result = collection.map_reduce(search_map(words), search_reduce, :query => criteria)
def search_reduce
"function( key , values ){return values[0];}"
end
def search_map(words)
#convert words into Regex OR