mongoid

Mongoid: ActiveModel Numericality Validation, allow_nil does not work

送分小仙女□ 提交于 2019-12-13 00:54:22
问题 I've defined a Mongoid model with an Integer field for which i validate numericality like this # source.rb class Source field :code, type: Integer validates_numericality_of :code, allow_nil: true The purpose of allow_nil is to validate fields which are present & ignore nil values. But here, allow_nil completely bypasses the numericality check object = Source.new object.code = "ABC" object.valid? => true object => #<Source _id: 50d00b2d81ee9eae46000001, _type: nil, code: 0> In activerecord,

Mongoid block until created?

痞子三分冷 提交于 2019-12-12 23:28:46
问题 I'm creating tests for an API with rspec using Mongoid and FactoryGirl. Mongoid is not a problem in production, but when testing API specs like so: it "renders [person]" do person = FactoryGirl.create(:person) get 'persons' json.count.should eq(1) end the model instance being created (in this case Person ) is sometimes not reflected in the database by the time the API call runs, resulting in an error like so: Failure/Error: json.count.should eq(1) expected: 1 got: 0 (compared using ==) How

belongs_to vs belongs_to_related, has_many vs has_many_related

不羁岁月 提交于 2019-12-12 19:24:15
问题 What does the _related add to a relation? For example what is the difference between belongs_to and belongs_to_related? I'm mostly seeing this in Mongoid apps but not sure if it applies to Rails in general as well. 回答1: belongs_to_related represents a relational association to a “parent” object. please check this issue and the example given here on GitHub: https://github.com/mongoid/mongoid/issues/348 See also: http://rubydoc.info/gems/mongoid-with-auth/1.9.4/Mongoid/Associations

mongodb: best way to get specific documents and then the rest

喜你入骨 提交于 2019-12-12 18:30:01
问题 lets say I have 1000 documents where each one has: user_id text Now, I would like to pull all those documents but first pull the documents from a few specific users (given an array of user ids) and then all the rest. I was thinking to use map reduce to create a new weight inline attribute if the user_id exists in the specific users array (using scope to pass the array) and then to sort that new attribute. But from what I could understand, you can not sort after map reduce. Any one has a good

How to make a query with or condition in mongoid

偶尔善良 提交于 2019-12-12 15:58:01
问题 How can I make a query with or condition in Mongoid. 回答1: Here is the solution for " OR " query in mongoid. if you want query like below select * from user where id = 10 or name = 'hitesh'; in rails with mongoid then you have to write query like this User.any_of({id: 10},{name: 'hitesh'}).first 回答2: This also works: User.or({id: 10},{name: 'hitesh'}) 回答3: @Simone Carletti is right but you also can use "mongo-style" notation: # Ruby 1.9+ Person.where(last_name: {"$in" => ["Penn", "Teller"]})

Rails 4.0.0.beta 1 and Mongoid

孤人 提交于 2019-12-12 14:52:44
问题 I am just trying out Mongodb and the latest rails setup. I am using Rails 4.0.0.beta 1, Ruby 2.0.0p0 and mongoid 4.0.0. I have a problem: I can create and delete objects as expected but cannot update objects. No error is returned, and the development log shows the changed parameters are being passed correctly. Has anyone else hit this issue? 回答1: Digging deeper I see that mongoid 4.0.0 is still referencing moped 1.4.5, which officially only supports ruby versions upto 1.9.3. The latest

How Do I Update Nested Mongo Document Attributes in Rails with Mongoid?

北战南征 提交于 2019-12-12 12:30:46
问题 (Apologies in advance if this question is short on details, I'll watch the comments and add what I can) I have a Model with the following: class Product include Mongoid::Document include Mongoid::Timestamps #... field :document_template, :type => Document accepts_nested_attributes_for :document_template Inside the Document document_template, is the following references_many, which I want to modify. Specifically, I want to change which fonts are referenced: class Document include Mongoid:

Mongoid has_and_belongs_to_many associations

守給你的承諾、 提交于 2019-12-12 11:03:46
问题 I trying to get mongoid to save associations, but I can only get one side to work. If I have the following test. test "should add a user as a follower when a user follows the group" do @cali_group.followers = [] @user1.followed_groups << @cali_group assert_equal 1, @user1.followed_groups.count assert_equal 1, @cali_group.followers.count end Which is failing, because @cali_group.followers is []. I've been working with this for awhile, tried @cali_group.reload . But it looks like the only way

Mongoid, how to order_by through a references_one association (and subsequent associations)?

大城市里の小女人 提交于 2019-12-12 09:45:04
问题 Simple model: class hat embedded_in :owner field :color end class owner embedds_one :hat referenced_in :house field :name end class house references_one :owner field :number end So simply puts, we have collection of houses that are associated to an owner, and the owner can have a colored hat. I can simply sort the house by their number: House.all.order_by( [[ :number, :asc ]]) But what I want is ordering the house, by the name of their owner, ideally I would like to write: House.all.order_by(

Formtastic with Mongoid embedded_in relations

本小妞迷上赌 提交于 2019-12-12 09:22:32
问题 Is there any quick way to make a form for embeds_many-embedded_in relation? I have the following: class Team include Mongoid::Document field :name, :type => String embeds_many :players end class Player include Mongoid::Document embedded_in :team, :inverse_of => :players field :name, :type => String end I want to create a form for team with embedded editing for players. Seen https://github.com/bowsersenior/formtastic_with_mongoid_tutorial but "TODO" there. 回答1: I wrote the formtastic_with