Skip to content

Instantly share code, notes, and snippets.

@marvin
Created January 20, 2012 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin/1647294 to your computer and use it in GitHub Desktop.
Save marvin/1647294 to your computer and use it in GitHub Desktop.
DataMapper repository issues
require 'rubygems'
require 'bundler'
Bundler.require
require './dmtrouble'
run Sinatra::Application
require "bundler"
enable :inline_templates
# DataMapper
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/dmtrouble.db")
DataMapper.setup(:mongo, "mongo://localhost:27017/dmtrouble")
DataMapper::Logger.new($stdout, :debug)
DataMapper.finalize
class Default
include DataMapper::Resource
property :id, Serial
property :name, String
end
DataMapper.auto_migrate!
DataMapper.repository(:mongo) {
class Mongotest
include DataMapper::Mongo::Resource
property :id, ObjectId
property :name, String
def self.default_repository_name
:mongo
end
end
DataMapper.finalize
}
# routes
get "/" do
@defaults = Default.all
@mongos = Mongotest.all
haml :index
end
get "/adddefault" do
newrecord = Default.create(:name => "new test record")
if newrecord.save
redirect '/'
end
end
get "/addmongo" do
newrecord = Mongotest.create( :name => "new mongo record" )
if newrecord.save
redirect '/'
end
end
__END__
@@layout
!!!
%html
%head
%title "datamapper trouble"
%body
#content= yield
@@index
%h1 default data
- if @defaults.empty?
%p no data
- else
- @defaults.each do |default|
%p= default.name
%h1 mongo data_mapper
- if @mongos.empty?
%p no data
- else
- @mongos.each do |mongo|
%p= mongo.name
source "http://rubygems.org"
gem 'sinatra', '~> 1.3.2', :require => 'sinatra'
gem 'data_mapper', '~> 1.2.0'
gem 'dm-mongo-adapter', :git => 'https://github.com/solnic/dm-mongo-adapter.git', :branch => "dm-1.2-compatible"
gem 'bson_ext', '~> 1.4.0'
gem 'mongo', '~> 1.4.0'
gem 'mongodb', '~> 2.0.0'
gem 'dm-sqlite-adapter'
gem 'haml'
@marvin
Copy link
Author

marvin commented Jan 20, 2012

issue was a nilclass error .... solved because DataMapper.finalize wasnt ran within the repository

afterwards i got an argumenterror for :id in mongotest this was solved by overwriting the default_repository_name with the mongo setup

@marvin
Copy link
Author

marvin commented Jan 20, 2012

you need bundle for this example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment