Mongoid gives uninitialized constant Mongo - ruby

I'm trying to use mongoid but it outputs this error:
uninitialized constant 'Mongo'
Here is my code:
require "mongoid"
Mongoid.configure do |config|
config.master = Mongo::Connection.new("localhost",27017).db("arthist")
end
class Artist
include Mongoid::Document
field :name, type: String
end
a = Artist.create(name: "hoge")
Do you have any idea?

include gem mongo in your Gemfile and restart the server it should do the trick.

Related

Use Geocoder with Sinatra and DataMapper

I'm attempting to use the Geocoder gem with a DataMapper model in a Sinatra application.
environment.rb:
require 'rubygems'
require 'bundler/setup'
require 'dm-core'
require 'dm-timestamps'
require 'dm-validations'
require 'dm-aggregates'
require 'dm-migrations'
require 'dm-types'
require 'geocoder'
require 'sinatra' unless defined?(Sinatra)
# load models
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
Dir.glob("#{File.dirname(__FILE__)}/lib/*.rb") { |lib| require File.basename(lib, '.*') }
DataMapper.setup(:default, (ENV["DATABASE_URL"] || "sqlite3:///#{File.expand_path(File.dirname(__FILE__))}/#{Sinatra::Base.environment}.db"))
DataMapper.finalize
DataMapper.auto_upgrade!
lib/location.rb:
class Location
include DataMapper::Resource
include Geocoder::Model::Base
property :id, Serial
property :address, String, :required => true
# geocoder gem
geocoded_by :address, :latitude => :lat, :longitude => :lng
# geocoder
after_validation :geocode, :if => :address_changed?
end
When I attempt to start an IRB session, an exception is generated:
irb> require './environment'
NameError: uninitialized constant Geocoder::Model
...
What am I not understanding?
First up, it looks like the Geocode gem won't have direct support for Datamapper, as per this issue.
Second, when you include a module inside a class, the methods are available to the instance of the class, and not at the class level. For example:
module Name
def name
puts "Module"
end
end
class SomeClass
include Name
end
SomeClass.new.name # => "Module"
This works because when you include a module, that module gets added to the ancestor chain of that class. Any methods that get sent to the instance which are not available on the instance are forwarded to the ancestors. However, there's another method called extend which adds the methods at a class-level rather than at instance level:
# module definition same as before
class SomeClass
extend Name
name # works!
end
Inorder to get class-level inclusion, there is another way (which is what the Geocoder gem uses for supported models:
# module code same as before
module Name
def name
puts "Module"
end
def self.included(klass)
klass.extend(self)
end
end
The included hook is provided for models which can be overridden to do something when the include Name step executes. Since there's no Datamapper specific module that is not executing this step, you see that error.

Factory Girl: NameError: uninitialized constant Vserver

I am trying to setup the unit testing with Factory Girl and Rspec for my Sinatra application.
Gem file:
group :test do
gem "rack-test"
gem "fuubar"
gem "factory_girl"
gem "yard"
end
spec/factories/vserver.rb
require 'factory_girl'
FactoryGirl.define do
factory :vserver do
first_name "John"
last_name "Doe"
end
end
spec/spec_helper.rb
require File.join(File.dirname(__FILE__), "..", "app.rb")
%w{
rubygems
sinatra
dm-core
rack/test
uuid
factory_girl
rspec
pp
spec/factories/vserver
}.each { |r| require r }
set :environment, :test
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
spec/app_spec.rb
require './spec_helper.rb'
require 'factory_girl'
describe "Cdot" do
include Rack::Test::Methods
def app
StorageApi
end
it 'vserver' do
FactoryGirl.build(:vserver)
end
end
Issue: When I run the rspec file using the command: rspec app_spec.rb, I get the below error.
NameError: uninitialized constant Vserver
Help is much appreciated.
the code: factory :vserver do assumes that you have a class called Vserver that you are instantiating.
If you have such a class, then you need to include it in your spec.
If you don't have such a class, then you either need to rename the factory, or tell it what class it should be instantiating instead.

Sinatra with Datamapper throws undefined method `add_domain_type' for Psych:Module (NoMethodError)

I'm working on a pretty simple sinatra + datamapper app, and it was launching and working just fine yesterday. I attempted to introduce some seed data into the db, and started getting this strange error on launch. I reverted back to pre-seed code, and the error persists. I've noticed that commenting out the include DataMapper::Resource in my model makes the app run again. here's the error:
/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/dm-serializer-1.2.2/lib/dm-serializer/to_yaml.rb:15:in included': undefined methodadd_domain_type' for Psych:Module (NoMethodError) ... Any ideas?
#app.rb:
require 'sinatra'
require 'pry'
require_relative 'models'
helpers do
def content_path(file)
File.join("images", file)
end
end
get '/' do
#photos = Photo.all
erb :index
end
models.rb
require 'data_mapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/db/photos.db")
class Photo
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :title, String
property :body, Text
property :filename, String
property :description, Text
property :width, String
property :height, String
end
DataMapper.finalize
Photo.auto_upgrade!
EDIT: It appears that upgrading ruby to 2.2.0 and reinstalling the gems solved the problem

How to use mongoid-rspec gem properly?

Following instructions I have installed mongoid-rspec and configured it in spec_helper.rb
RSpec.configure do |config|
config.include Mongoid::Matchers, type: :model
end
But came across an issue with the simplest test
describe City do
it { should have_many(:locations) }
end
City should have many :locations
Failure/Error: it { should have_many(:locations) }
NoMethodError:
undefined method `has_many?' for
Seems I'm doing it wrong, but can't figure out what's exactly wrong.
The models are pretty simple
class City
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
include Mongoid::Versioning
max_versions 10
has_many :locations
end
class Location
include Mongoid::Document
belongs_to :city
field :name, type: String
end
One last thing, I don't use Rails.
I came across similar issue but whilst working on rails, solution for both rails and none rails I believe are very similar:
gem 'mongoid-rspec', '~> 2.2.0'
Without rails
Add to your spec_helper.rb file:
require 'mongoid-rspec'
RSpec.configure do |config|
config.include Mongoid::Matchers
end
With rails
Add to your rails_helper.rb
require 'mongoid-rspec'
RSpec.configure do |config|
config.include Mongoid::Matchers, type: :model
end
In my spec_helper file i removed the option type: :model and it now works
RSpec.configure do |config|
config.include Mongoid::Matchers
end

MongoId - uninitialized constant App::Mongo (NameError)"

In Sinatra app I have
#config.rb
require 'mongoid'
class App
configure do
Mongoid.configure do |config|
name = "my_db"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.slaves = [Mongo::Connection.new(host, 27017, :slave_ok => true).db(name)]
config.persist_in_safe_mode = false
end
end
end
#Gemfile
gem "mongoid", "~> 3.0.0"
gem "bson_ext"
and it gives me an error "`const_missing': uninitialized constant App::Mongo (NameError)"
How do I fix it?
This is due to the fact that Mongoid 3.x no longer uses the 10Gen Ruby driver so Mongo module will not be loaded by require 'mongoid'. You need to use the new Mongoid.load! method. You may have to change your config.yml file a bit as the syntax has changed. Please see http://mongoid.org/en/mongoid/docs/installation.html

Resources