Tag: mongoid

field_for和带有mongoid的嵌套表单

有人能给我一个使用mongoid的嵌套表单的工作示例吗? 我的模特: class Employee include Mongoid::Document field :first_name field :last_name embeds_one :address end class Address include Mongoid::Document field :street field :city field :state field :post_code embedded_in :employee, :inverse_of => :address end

rails mongoid标准通过关联找到

我正在尝试通过关联的用户名来查找记录,该用户名包含在belongs_to关系中,但它不起作用。 文章属于用户用户有很多文章 Article.where(user_id: someid)工作正常,但我想使用用户名作为参考,存储在Users表中。 Article.includes(:user).where(:username => “erebus”) Article.includes(:user).where(“user.username” => “erebus”) 我也有identity_map_enabled: true Article.includes(:user).inclusions返回关系详细信息 不起作用,我不理解什么?

如何从rails应用程序上的ruby获取mongodb数据库和集合列表的列表

我正在使用Rails 3和Mongoid gem。 但我需要用mongodb数据库列表填充一个combobox。 在mongodb shell中,我们可以使用“show dbs”命令列出数据库。 mongodb驱动程序中还有getDBNameList()和db.getCollectionNames()命令。 但我无法弄清楚如何在rails应用程序上使用来自ruby的这些命令。 我也在想; 如果我可以使用mongoid gem获取数据库和集合列表。 因为我确信我已经读过mongoid支持使用多个数据库,但我认为它依赖于模型。 所以你怎么看; 有什么解决方案,或者我必须使用mongo-ruby-driver gem,而不是mongoid。

查询Mongoid / rails 3中的嵌入对象(“低于”,Min运算符和排序)

我正在使用带有mongoid的rails 3。 我有一个股票的集合与嵌入的价格集合: class Stock include Mongoid::Document field :name, :type => String field :code, :type => Integer embeds_many :prices class Price include Mongoid::Document field :date, :type => DateTime field :value, :type => Float embedded_in :stock, :inverse_of => :prices 我想得到自给定日期以来最低价格低于给定价格p的股票,然后能够对每种股票的价格进行排序。 但看起来Mongodb不允许这样做。 因为这不起作用: @stocks = Stock.Where(:prices.value.lt => p) 此外,似乎mongoDB无法对嵌入对象进行排序。 那么,有没有替代方案来完成这项任务? 也许我应该将所有内容放在一个集合中,以便我可以轻松运行以下查询: @stocks = Stock.Where(:prices.lt => p) 但我真的想在我的查询之后得到按股票名称分组的结果(例如,具有一系列有序价格的不同股票)。 […]

HABTM mongoid追随者/追随者

Mongoid在一个habtm上发布了.push,它在两个方向上建立了一个habtm关系。 虽然删除将#delete一个关联的记录,但没有记录的方法只删除我见过的关系。 有更好的方法吗? 有没有更好的方法来确保独特性? has_and_belongs_to_many :following, {class_name: ‘User’, inverse_of: :followers, inverse_class_name: ‘User’} has_and_belongs_to_many :followers, {class_name: ‘User’, inverse_of: :following, inverse_class_name: ‘User’} def follow!(user) self.following.push(user) # this pushes the inverse as well self.following_ids.uniq! self.save! user.follower_ids.uniq! user.save! end def unfollow!(user) self.following.delete(user.id) self.save! user.followers.delete(self.id) user.save! end

失败,错误10068:“运算符无效:$ oid”

刚更新到最新版本的mongoid(3.1.0)和sidekiq(2.7.2) 现在我收到错误: failed with error 10068: “invalid operator: $oid” 从查看代码我看到一个像这样的选择: @selector={“_id”=>{“$oid”=>”[some id]”}} 什么可以在这里出错?

在rails app中使用mongoid的Mongo查询导致游标超时错误

我在我的rails应用程序中有一个mongo查询,因为该集合非常庞大。 FbCheckin.where(ext_fb_place_id: self.ext_fb_place_id).all 我从文档中读到,您可以添加一个timeout选项,以防止光标timeout ,并显示以下消息: Moped::Errors::CursorNotFound: The operation: “GET MORE” failed with error 我尝试了几种方法,包括 FbCheckin.where(ext_fb_place_id: ext_fb_place_id, {:timeout=>false}).all 和 FbCheckin.find(ext_fb_place_id: ext_fb_place_id, {:timeout=>false}).all 但这些都不会阻止光标超时。 有没有人知道如何进行此查询并收集所有FbCheckins而不事先将光标超时? 谢谢

heroku mongohq和mongoid Mongo :: ConnectionFailure

2012年6月9日更新: 在heroku上使用mongoid 3.0.0.rc进行设置,请参阅此要点: https ://gist.github.com/2900804 2011年1月22日更新: Uri现在优先于mongoid.yml https://github.com/mongoid/mongoid/issues/issue/266 更新2010年8月12日:虽然我得到了Jackues Crocker的第6个可接受的答案,但是这个问题的某些方面很容易搞砸! 它再次发生在我身上,我决定研究mongoid源代码。 所以,在这里: 目前, 主机:端口:名称/数据库:设置在uri:设置上取得了优先权 。 因此,由于对localhost:xxxx的请求而不是对flame.local.mongohq.com:xxxx的请求,发生了非常无法提供信息的错误消息 这会打破! defaults: &defaults host: localhost <- THIS 'OVERWRITES' host in the uri! production: <<: *defaults <- BE CAREFUL WITH WHAT YOU BRING IN. THE host: FROM DEFAULTS WILL BE THE ONE APPLIED, not your uri host. uri: 修复它,删除主机:默认情况下,和/或删除<<:*默认值 原文问: […]

在Rails 3.2中使用Mongoid时删除database.yml

我刚开始一个新的rails项目,想通过Mongoid gem使用MongoidDB。 按照Mongoid站点上的说明,我将以下行添加到我的Gemfile : gem “mongoid”, “~> 2.4” gem “bson_ext”, “~> 1.5” 然后我按照此处的说明继续删除我的database.yml文件。 我的application.rb文件现在看起来像这样: require “action_controller/railtie” require “action_mailer/railtie” require “active_resource/railtie” require “rails/test_unit/railtie” require “sprockets/railtie” # Uncomment this line for Rails 3.1+ 现在,当我使用rails s在开发中启动我的服务器时,我收到以下错误: ~/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.0/lib/rails/railtie/configuration.rb:85:in `method_missing’: undefined method `active_record’ for # (NoMethodError) 我试着找一个解决方案,但似乎还没有人遇到我的问题。 难道我做错了什么? 这是由最近的Rails 3.2更新引起的吗? 谢谢你的帮助! 更新(1月26日):根据Dylan Markow的信息,我使用了终端命令 grep -r active_record config/ 并在评论块中将任何引用放入active_record。 我有一个简单的控制器,其中一个动作甚至还没有打到数据库。 当我通过浏览器访问操作时,我得到了 […]

Rails 3:如何同时使用活动记录和mongoid

我读过很多人建议将nosql与sql数据存储区一起使用。 例如,在mysql中有一些报告审计跟踪或日志信息,在mongodb中有一些线程分层数据。 是否有可能在mysql和mongoid上挂起活动记录的rails? 开箱即用它似乎不起作用…任何提示? 或者这是一种不推荐的方法吗?