Tag: mongoid

如何使用MongoDB和Mongoid在Rails 3上进行适当的数据库测试(TDD)

如何通过Mongoid on Rails使用MongoDB编写适当的unit testing(以及针对该问题的集成测试)? 我问,因为与使用SQLite3相反,即使在运行测试时,我所做的一切仍然存在。 所以目前我正在编写创建测试,然后手动删除我所做的一切。 但是对于集成测试来说,它变得很烦人甚至很复杂。 我做的样本: before(:each) do @user = User.create!(@attr) end after(:each) do # MongoDB is not a transactional DB, so added objects (create) during tests can’t be rollbacked # checking for the existance of a similar object with exact :name and :email (regex make it case insensitive) cleanup = User.where(:name => […]

如何在Rails 3中并行使用Mongoid和ActiveRecord?

我正在使用rails 3,并使用ActiveRecord开始我的应用程序。 现在,我有很多模型,关系开始变得复杂,有些可以用Document-Oriented结构更简单地表达,所以我想尝试迁移到MongoDB并使用Mongoid。 我一直听说你没有必须使用所有MongoDB或者没有任何东西,但你可以在迁移时并行使用这两个。 我不知道如何从文档中解决这个问题。 例如,我有: class User :items end class Product < ActiveRecord::Base has_many :items end class Item < ActiveRecord::Base belongs_to :user belongs_to :product # alot of data that fits a hierarchical document-oriented structure end 我想在理想情况下首先用Mongoid文档替换我的Item activerecord模型,这样我的项目就存储在MongoDB中,我的Users和Products可以保留在我的SQL DB中 事情是,我不知道该怎么做。 我是以正确的方式来做这件事的吗? 也许另一种选择是保留基础AR项目 class Item < ActiveRecord::Base has_one :mongodb_item ?? # I know this is wrong […]

方法适用于开发但不适用于生产Rails MongoDB

我有优惠券课程,我希望我的应用程序检查并查看优惠券上剩余的计数以及优惠券的日期是否已过期。 我class上有以下方法来检查这两个方法。 Coupon class def self.get(code) where( :code => (normalize_code(code)), :$and => [ { :$or => [ { :coupon_count.gte => 1 }, { :coupon_count => nil } ] }, { :$or => [ { :expires_at.gt => Time.now.utc }, { :expires_at => nil } ] } ] ).first end 当我输入优惠券时,这在开发中工作正常。 但在生产中它不起作用。 我使用我的MongoDB shell创建优惠券,如下所示。 db.Coupon.insert({code:’#COUPONNAME’,discount_percent: 10, expires_at: […]

什么inverse_of在mongoid中意味着什么?

inverse_of在mongoid关联中的含义是什么? 我可以通过使用它而不仅仅是没有它的关联来获得什么?

无法让mongoid使用Rails 4

我按照官方教程 。 我在我的Gemfile中注释了sqlite3以及以下行: gem ‘mongoid’, ‘~> 4’, github: ‘mongoid/mongoid’ gem ‘bson_ext’ 但是,我一直收到Specified ‘sqlite3’ for database adapter, but the gem is not loaded. Add gem “sqlite3” to your Gemfile. Specified ‘sqlite3’ for database adapter, but the gem is not loaded. Add gem “sqlite3” to your Gemfile. 原因似乎是database.yml仍然将sqlite列为数据库。 我怎么能让Rails使用生成的mongoid.yml? 用mongoid.yml替换database.yml的内容似乎没有办法 – 我得到了 ActiveRecord::AdapterNotSpecified: database configuration does not […]

管理mongoid迁移

有人能给我一个简短的介绍,使用Mongoid在Rails中进行数据库迁移吗? 我对每个文档迁移的懒惰特别感兴趣。 这意味着,无论何时从数据库中读取文档,都可以将其迁移到最新版本并再次保存。 有没有人以前做过这种事情? 我遇到过mongoid_rails_migrations ,但它没有提供任何类型的文档,虽然它看起来像这样做,但我不确定如何使用它。 我应该指出,我只是在概念上熟悉ActiveRecord迁移。

获得至少一个关联对象的记录

我在mongoid中有以下架构: 用户有很多任务 – has_many:tasks 任务属于user – belongs_to:user 如何只有10个第一个用户至少有一个任务? 像这样的东西: User.where(:tasks.ne => [] ).limit(10)

mongoid和mongo之间的冲突(mongo-ruby-driver)

我需要在我的Rails应用程序中使用两个gemmongoid和mongo 。 任何想法解决这个麻烦的问题谢谢〜 gem ‘mongo’, ‘2.0.2’,:git => ‘git://github.com/mongodb/mongo-ruby-driver.git’ gem ‘mongoid’, ‘4.0.0’ # gem “mongo_mapper” 错误信息 Bundler could not find compatible versions for gem “bson”: In Gemfile: mongo (= 2.0.2) ruby depends on bson (~> 3.0) ruby mongoid (= 4.0.0) ruby depends on moped (~> 2.0.0) ruby depends on bson (~> 2.2) ruby UPDATE Mongoid很方便,但有时很糟糕, 因为我收到错误’Client […]

MongoDB不会使用Mongoid连接到MongoHQ

我刚刚开始了一个全新的rails项目,我正在尝试完成的第一项任务是创建一个对象并将其保存在我的数据库中。 我经历了生成对象的自动方式,在这种情况下是一个URL对象,如下所示: rails generate scaffold Url domain:string 我还测试了两个单独的mongoid.yml配置设置。 第一个如下: development: sessions: default: # Defines the name of the default database that Mongoid can connect to. # (required). uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database] options: consistency: :strong 此配置的结果是,当发生查询时,我收到以下错误: The operation: # failed with error 13075: “db name can’t be empty” 我还测试了以下配置: development: uri: mongodb://[username]:[password]@flame.mongohq.com:27046/[database] 当代码到达urls_controller的create方法时,它在@ url.save失败,并出现以下错误: mongoid (3.0.0.rc) lib/mongoid/sessions/factory.rb:100:in `parse’ […]

我正在使用rails cast omniauth,我收到此错误

我使用Mongodb作为rails中的数据库,使用/ auth / linkedin / callback时出错 AuthenticationsController#中的NoMethodError为nil创建未定义的方法[]’:NilClass Rails.root:/ home / prem / Music / heronhrm Application Trace | 框架跟踪| 完整跟踪app / models / user.rb:57:在apply_omniauth’app / controllers / authentications_controller.rb:19:在’create’中 当我删除self.email = omniauth[‘user_info’][’email’] if email.blank? 从usermodel然后出现validation错误 users / sign_up电子邮件不能为空我想为twitter,linkdin和facebook实现。 我的身份validation.rb class Authentication include Mongoid::Document belongs_to :user field :user_id, :type => String field :provider, :type => String field […]