mongoid

How can I find embedded Mongoid documents based on multiple criteria?

喜夏-厌秋 提交于 2019-12-07 12:14:43
问题 I have a Mongoid document with embedded documents in it. I want to search the top-level documents for all of them where there is an embedded document that has multiple criteria. TopDoc.where('inside.first_name' => 'Bob', 'inside.last_name' => 'Jones') But it seems to me this would match on a TopDoc with Bob Wever and Paul Jones, which is wrong. 回答1: You need to use $elemMatch. With Mongoid, the following line should do the trick TopDoc.elem_match(inside: { first_name: 'Bob', last_name: 'Jones

Ruby on Rails 4: test nested resources with rspec not working

爷,独闯天下 提交于 2019-12-07 08:38:54
问题 i have some troubles with testing my ruby on rails application. I have a resource Restaurant and a nested resource menu. Routes file: resources :restaurants do resources :menus Menu model: class Menu include Mongoid::Document belongs_to :restaurant accepts_nested_attributes_for :restaurant validates :name, presence: true validates :description, presence: true validates :restaurant, presence: true field :name, type: String field :description, type: String end restaurant model: class Restaurant

Undefined method “getlocal” when displaying created_at?

老子叫甜甜 提交于 2019-12-07 07:58:46
问题 i use rails 3.2.6, ruby 1.9.3 with the mongoid gem 3.0. i want to display the created_at field of a database entry, but get the following error: undefined method `getlocal' for "Wed, 25 Apr 2012 15:04:37 -0400":String here's the rails code: <dt>Erstellt am:</dt><dd><%= @app.created_at %></dd> any advice what's the problem? is there a bugfix? should work in my opinion? thanks in advance! 回答1: try adding the following to your model include Mongoid::Timestamps see http://mongoid.org/en/mongoid

Making an entire model read-only with Mongoid

拟墨画扇 提交于 2019-12-07 07:22:23
问题 I see that Mongoid supports read-only attributes. Is there a way to mark an entire document, or an entire collection / model class as read-only? 回答1: You can access the fields class attribute and splat the hash's keys against attr_readonly . For example: class Model include Mongoid::Document attr_readonly *fields.keys end Note that, fields.keys will include _id and _type . 来源: https://stackoverflow.com/questions/14573914/making-an-entire-model-read-only-with-mongoid

When to index, what to index in Mongoid?

非 Y 不嫁゛ 提交于 2019-12-07 06:41:33
问题 I'm a bit new to indexes, but I am curious about the usecases for an index. (I assume it makes queries on the indexed fields much faster.) Is there a criteria for determining what to index and when to index? What kind of performance benefits should I expect -- specifically using Mongoid with MongoDb on a Rails app? 回答1: Check out Indexes MongoDB docs and Indexing Advice and FAQ, lots of great info here. 来源: https://stackoverflow.com/questions/6183945/when-to-index-what-to-index-in-mongoid

remove an embedded document in mongoid

巧了我就是萌 提交于 2019-12-07 06:14:14
问题 I have a projects model with just a name field and in it also the embedded relation to line_items. class Project include mongoid::document field :name embeds_many :line_items end class LineItem include mongoid::document field :title embedded_in :project, :inverse_of => :line_items end I suppose this is more of the mongo driver question: if I had such a document db.project.find()[0] { _id : 123, name : "housework", line_items:[ { title : "clean fridge", _id : 601}, { title : "clean tub", _id :

Mongoid: How to prevent undefined fields from being created by mass assignment?

前提是你 提交于 2019-12-07 05:56:48
问题 Here's the code: class M include Mongoid::Document field :name end params = { name: "foo", age: 20 } M.create(params) #=> #<M name: "My Name", age: 20> Notice that age wasn't defined, yet it was saved. This is problematic (potentially a source of DoS) because a malicious user can add any parameters in POST and unknown fields with a large string can sneak in. (e.g. name=foo&bogus=#{'x'*1000000} ) So far, I couldn't find anything but attr_accessible , but it's not really great for Mongoid as

rails mongoid clear criteria

白昼怎懂夜的黑 提交于 2019-12-07 03:16:26
问题 Mongoid::Paranoia adds a default scope to the model which generates the criteria #<Mongoid::Criteria selector: {:deleted_at=>{"$exists"=>false}}, options: {}, class: Line, embedded: false> I can find deleted documents with Model.deleted which generates, #<Mongoid::Criteria selector: {:deleted_at=>{"$exists"=>true}}, options: {}, class: Line, embedded: false> How can i override this so i can search both deleted and non-deleted documents. PS Model.unscoped does not work 回答1: Try this(its kind

Embedding message replies inside message parent with mongodb using mongoid

丶灬走出姿态 提交于 2019-12-06 23:46:38
I think it's a best practice to embed replies to a specific message inside that message and I'm trying to implement it using mongoid. here is what I have class Message include Mongoid::Document include Mongoid::Timestamps include Mongoid::Paranoia field :subject field :body field :sender_deleted, :type => Boolean, :default => false field :recipient_deleted, :type => Boolean, :default => false field :read_at, :type => DateTime referenced_in :sender, :class_name => "User", :inverse_of => :sender, :foreign_key => 'sender_id' referenced_in :recipient, :class_name => "User", :inverse_of =>

How do I exclude fields from an embedded document in Mongoid?

落花浮王杯 提交于 2019-12-06 20:06:11
问题 I have a Post document that has embedded tags. Sometimes I display only the title of a post and its tags. In those cases, I use the following query in mongoid: Post.only(:title).find(id) I then send the results of the query as json to the client. Unfortunately, the tag's bson id makes the json much larger than I need it to be. How do I exclude the "_id" field from the query? Here are my models: class Post include Mongoid::Document field :title, :type => String field :body, :type => String