【发布时间】:2013-02-13 13:41:27
【问题描述】:
我尝试在我的项目中使用 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')
【问题讨论】:
-
您尝试使用 Datamapper 作为 Mongodb 的 ORM 背后的原因是什么?你试过Mongoid 吗?
-
我试过 Mongoid,效果很好。事实上,datamapper.org 上提到了 3 个适配器,例如 dm-sqlite-adapter、dm-mysql-adapter、dm-postgres-adapter,而且我也看不到 dm-mongo-adapter。另一方面,使用 dm-mongo-adapter 有什么好处或坏处?
标签: ruby-on-rails ruby mongodb datamapper ruby-datamapper