mongoid

    mongoid 3.0.x not set relation properly

    08 Sep 2013

    I was trying to fix bullet test failure with mongoid 3.0.23, the failed test is to test the 1-1 relationship as follows

    it "should detect non preload association" do
      Mongoid::Company.all.each do |company|
        company.address.name
      end
      ......
    end

    After reading the logs, it generated 2 unexpected query

    MOPED: 127.0.0.1:27017 QUERY        database=bullet collection=mongoid_companies selector={} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (196.5840ms)
    MOPED: 127.0.0.1:27017 QUERY...
    Read More

    Tags 


    Upgrade Mongoid - Multiple databases

    22 Mar 2011

    My recent post Use different mongodb instances in mongoid tells you how to use multiple databases, it looks good, but mongoid began to support multiple databases itself from mongoid.2.0.0.rc.1, much better than my hack.

    It's really easy to use, first, you should define multiple databases in mongoid.yml like

    development:
      <<: *defaults
      host: localhost
      database: main_mongo_instance
      databases:
        other_mongo_instance_name:
          database: other_mongo_instance
          host: localhost

    As you seen, besides the common database param, I have defined a new param databases, you should define the mongo instance name with database and host name, and of course, you can define as many mongo instances as you ne...

    Read More

    Tags 


    Upgrade Mongoid - update_attribute

    21 Mar 2011

    Before mongoid 2.0.0.rc.6, there is no update_attribute method for Mongoid::Document, it makes me unhappy. As in ActiveRecord world, I always use update_attribute to change one attribute and use update_attributes to change two or more attributes.

    It's a good news that mongoid introduces the update_attribute method from 2.0.0.rc.6, now I can follow my practice in mongoid.

    post.update_attribute(:title => "New Post")
    
    post.update_attributes(:title => "New Post", :body => "New Body")
    Read More

    Tags 


    Upgrade Mongoid - Many to many association

    08 Mar 2011

    Before mongoid 2.0.0.rc1, there is no default support for many to many association. So we use join document (aka join table in relational database) to implement the many to many association.

    For example, we have two documents users and accounts, one user has many accounts and one account contains many users, to establish the many to many relationship between users and accounts, we create a new document named user_accounts, the document looks like

    {'_id': '4d76d3a70bdb822d08000001', 'user_id': BSON...
    Read More

    Tags 


    Upgrade Mongoid - Hash arguments for group

    01 Mar 2011

    You will receive a warning for the group method call after upgrading mongoid.

    Collection#group no longer take a list of paramters. This usage is deprecated.

    exactly this is because mongo gem changes the group method definition.

    Before

    key = ["ad_id"]
    conditions = { 'ad_id' => { '$in' => ad_ids } }
    initial = { "impressions" => 0.0, "clicks" => 0.0 }
    reduce = "a reduce javascript function"
    
    AdStat.collection.group(key, conditions, initial, reduce).each do |e|
      ......
    end

    After

    key = ["ad_i...
    Read More

    Tags 


    Upgrade Mongoid - Default Type for Field

    28 Jan 2011

    If you have watched the episode about mongoid from railscast, ryanb removed the default type String for field, like

    class Article
      field :name, :type => String
      field :content, :type => String
    end

    can be written as

    class Article
      field :name
      field :content
    end

    but it is not valid from mongoid.2.0.0.rc.1 again, the default type of field is changed from String to Object, that means we should explicitly set the type for each field.

    Read More

    Tags 


    Upgrade Mongoid - Write Tests First

    27 Jan 2011

    Mongoid is one of the popular Object Document Mappings between Ruby and Mongo, and it is still evolving. We began to use mongoid 2.0.0.beta.20 several weeks ago, the author of mongoid @durran said he wanted to release the 2.0.0 last week (As you know 2.0.0 is still not released yet, but he really did a lot of awesome work), so we tried the version 2.0.0.rc.6 to prepare upgrading to final 2.0.0.

    I'm working on upgrading mongoid from 2.0.0.beta.20 t...

    Read More

    Tags 


    Use different mongodb instances in mongoid

    14 Jan 2011

    By default, we save all the collections in one mongodb instance, or replicate/shard all of them into different mongodb instances. But what if saving a special collection into one mongodb instance, and the other collections into the other mongodb instance?

    This is what I need to do with a mongoid project several weeks before. In common we just define a mongodb instance in the config/mongoid.yml, yep, you can define only one inst...

    Read More

    Tags