我尝试在我的项目中使用datamapper和mongoid。我跟踪了链接https://github.com/solnic/dm-mongo-adapter。但是没有这么多的信息。我在这篇文章中同化了datamapper和sqlite3适配器:http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-working-with-datamapper/一切都可以用sqlite3,但是我陷入了mongodb的泥潭。
当我在控制台中运行"ruby rm.db“时,我会接受"dm.rb:1:in‘:(NameError)”错误。
我如何解决这个问题?我在下面的宝石文件中添加了这些宝石:
dm-core
dm-aggregates
dm-migrations
mongo
mongodb
mongo_ext 然后,我在项目根目录中的一个名为dm.rb的文件中添加了以下代码。
DataMapper.setup(:default,
:adapter => 'mongo',
:database => 'my_mongo_db',
)
# Define resources
class Student
include DataMapper::Mongo::Resource
property :id, ObjectId
property :name, String
property :age, Integer
end
class Course
include DataMapper::Mongo::Resource
property :id, ObjectId
property :name, String
end
# No need to (auto_)migrate!
biology = Course.create(:name => "Biology")
english = Course.create(:name => "English")
# Queries
Student.all(:age.gte => 20, :name => /oh/, :limit => 20, :order => [:age.asc])
# Array and Hash as a property
class Zoo
include DataMapper::Mongo::Resource
property :id, ObjectId
property :opening_hours, Hash
property :animals, Array
end
Zoo.create(
:opening_hours => { :weekend => '9am-8pm', :weekdays => '11am-8pm' },
:animals => [ "Marty", "Alex", "Gloria" ])
Zoo.all(:animals => 'Alex')发布于 2013-03-08 00:28:47
我给你分两部分回答。
首先,为了解决当前的问题,问题是在尝试使用它之前,它看起来并不需要DataMapper。您可以在rb文件的顶部要求dm-mongo适配器,或者因为您使用的是绑定器,所以可以在Gemfile中直接这样做。
# add this to the beginning of your dm.rb file
require 'dm-mongo-adapter'
# or put this in your Gemfile, run with `bundle exec dm.rb`
gem 'dm-mongo-adapter', :require => true第二,关于dm-mongo适配器的使用.这种方法有几个问题,可能会让你现在和以后都头疼。
https://stackoverflow.com/questions/14854918
复制相似问题