Tag: mongoid

Mongoid和ActiveRecord关系:未定义的方法`quoted_table_name’

class Contest NoMethodError: undefined method `quoted_table_name’ for ClaimTemplate:Class 好的,我们将quoted_table_name添加到ClaimTemplate : def self.quoted_table_name “claim_templates” end # console Contest.new.claim_template #=> nil # Cool! # But: Contest.last.claim_template #=> TypeError: can’t convert Symbol into String 那么如何配置我的模型以便彼此正常工作 PS: 现在我有这种结构,工作正常,但我希望获得关系( Assosiations )的好处。 class Contest self.id).first end # Mongoid going to be crazy without this hack def self.using_object_ids? false end end

如何从Mongoid中的嵌入文档中排除字段?

我有一个包含嵌入标签的Post文档。 有时我只显示post的标题及其标签。 在这些情况下,我在mongoid中使用以下查询: Post.only(:title).find(id) 然后我将查询结果作为json发送给客户端。 不幸的是,标签的bson id使得json比我需要的大得多。 如何从查询中排除“_id”字段? 这是我的模特: class Post include Mongoid::Document field :title, :type => String field :body, :type => String field :tags, :type => Array embeds_many :tags end class Tag include Mongoid::Document field :tag, :type => String field :type, :type => String embedded_in :post end

有没有办法在使用mongodb / mongoid排序时将所有nil值放在最后?

真的没什么可说的,而不是问题中的内容。 使用mongoid: People.asc(:age) 我先得到零值。 有没有办法总是最后返回nil,或告诉mongodb将nil视为非常高? 完全像在这里sql中的相同问题的答案

Mongoid不在查询中

我对mongoid有些麻烦: test “Test candidate” do User.create(:id => 1, :sex => User::Male, :country => 1, :city => 1) User.create(:id => 2, :sex => User::Female, :country => 1, :city => 1) User.create(:id => 3, :sex => User::Female, :country => 1, :city => 1) user = User.not_in(:id => [2]).second assert_not_equal(user.id, 2) end 测试失败。 我曾尝试使用where(:id => {‘$ nid’=> [2]}),但它具有相同的效果。 […]

使用elasticsearch来过滤带有空格的标签

我正在使用轮胎(https://github.com/karmi/tire)与mongoid。 这是我的模型定义: class SomethingWithTag include Mongoid::Document include Mongoid::Timestamps field :tags_array, type: Array include Tire::Model::Search include Tire::Model::Callbacks mapping do indexes :tags_array, type: :array, index: :not_analyzed end end 说我有一个文件{tags_array:[“hello world”]}。 然后以下查询工作正常: SomethingWithTag.tire.search { filter :terms, :tags_array => [“hello”] } SomethingWithTag.tire.search { filter :terms, :tags_array => [“world”] } SomethingWithTag.tire.search { filter :terms, :tags_array => [“hello”, “world”] } 但以下内容不会返回任何结果: […]

列出Mongoid模型中的动态属性

我已经阅读了文档,我找不到具体的方法来解决这个问题。 我已经为模型添加了一些动态属性,我希望能够遍历所有这些属性。 所以,举一个具体的例子: class Order include Mongoid::Document field :status, type: String, default: “pending” end 然后我做以下事情: Order.new(status: “processed”, internal_id: “1111”) 后来我想回来并且能够获得所有动态属性的列表/数组(在这种情况下,“internal_id”就是它)。 我还在挖,但我很想听听其他人是否已经解决了这个问题。

Mongoid随机文件

让我们说我有一个用户集合。 有没有办法使用mongoid在集合中找到n个随机用户,它不会返回同一个用户两次? 现在让我们假设用户集合如下所示: class User include Mongoid::Document field :name end 简单吧? 谢谢

MongoDB和Mongoid正在制作中

我正在部署我的第一个小应用程序MongoDB和Mongoid作为驱动程序。 在生产中使用MongoDB的正确安全方法是什么? 我的意思是在开发中我刚刚开始使用mongod ,就是这样 – 不需要用户名或密码,看起来不安全。 Mongoid也设置默认配置 production: host: port: username: password: database: 我应该如何在生产服务器上配置此选项和整个MongoDB?

如何获取Mongoid文档的所有字段名称?

我正在构建后端系统,如Iain Hecker的教程中所述: http ://iain.nl/backends-in-rails-3-1,我尝试使用Mongoid将其改编为MongoDB。 所以,当我需要写入backend / resourse_helper.rb时 module Backend::ResourceHelper def attributes resource_class.attribute_names – %w(id created_at updated_at) end end 我收到以下错误: undefined method `attribute_names’ for Backend::User:Class (我将后端植根于“后端/用户#index”)。 后端::用户inheritance自用户: class User include Mongoid::Document devise_for :users field :name field :address end 我只需要该用户的字段列表:类,我猜(即[“电子邮件”,“名称”,“地址”,……]),但我试图找到方法。

覆盖Mongoid模型的setter和getter

有没有办法在Mongoid中覆盖模型的setter或getter? 就像是: class Project include Mongoid::Document field :name, :type => String field :num_users, type: Integer, default: 0 key :name has_and_belongs_to_many :users, class_name: “User”, inverse_of: :projects # This will not work def name=(projectname) @name = projectname.capitalize end end 在不使用虚拟字段的情况下可以覆盖name方法吗?