我尝试在我的项目中使用 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 `': uninitialized constant DataMapper (NameError)”错误。
我该如何解决这个问题?我在下面的 gemfile 中添加了这些 gem:
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')