rubykaigi presentation

My presentation in RubyKaigin 2011 today.

and the video is here: http://www.ustream.tv/recorded/16051491

Posted in  presentation ruby rails


beijing ruby线下活动

周日在北京的ruby线下活动的ppt

Posted in  presentation


Upgrade Mongoid - Multiple databases

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 need.

Then, you can choose which mongo instance to use in mongoid model.

class User
  include Mongoid::Document

  set_database :other_mongo_instance_name
end

set_database method tells mongoid that the model will use another mongo instance instead of the main mongo instance, here we use the name other_mongo_instance_name which should exactly be the same with the name defined in mongoid.yml. If you don't say anything, it will use the main_mongo_instance.

So all the users data will be stored to other_mongo_instance_name, and the other data will be stored to main_mongo_instance. Great!

Posted in  mongoid ruby


Upgrade Mongoid - update_attribute

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")

Posted in  mongoid ruby


Upgrade Mongoid - Many to many association

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::ObjectId('4d76d3b80bdb822d080015b3'), 'account_id': BSON::ObjectId('4d76d3b90bdb822d080015b7')}

and the models are defined as follows

class User
  include Mongoid::Document
  references_many :user_accounts
end

class Account
  include Mongoid::Document
  references_many :user_accounts
end

class UserAccount
  include Mongoid::Document
  referenced_in :user
  referenced_in :account
end

Are you familiar with it, it's what activerecord did for many to many association.

I'm glad that mongoid began to support many to many association after mongoid 2.0.0.rc1, the new syntax is "referenes_and_referenced_in_many".

class User
  include Mongoid::Document
  references_and_referenced_in_many :accounts
end

class Account
  include Mongoid::Document
  references_and_referenced_in_many :users
end

We don't need the join document any more. The implementation of mongoid is different with activerecord, it uses array attribute to store the relationship at both sides. Like

These are user documents

{'_id': '4d76d3a90bdb822d08000009', account_ids: [BSON::ObjectId('4d76d3aa0bdb822d0800001b'), BSON::ObjectId('4d76d3aa0bdb822d0800001d'), BSON::ObjectId('4d76d3aa0bdb822d08000017')]}
{'_id': '4d76d3a80bdb822d08000005', account_ids: [BSON::ObjectId('4d76d3aa0bdb822d08000017'), BSON::ObjectId('4d76d3a90bdb822d08000015')]}

And these are account documents

{'_id': '4d76d3aa0bdb822d08000017', user_ids: [BSON::ObjectId('4d76d3a80bdb822d08000005'), BSON::ObjectId('4d76d3a90bdb822d08000009'), BSON::ObjectId('4d76d3a90bdb822d0800000d')]}
{'_id': '4d76d3aa0bdb822d0800001b', user_ids: [SON::ObjectId('4d76d3a90bdb822d08000009'), BSON::ObjectId('4d76d3a90bdb822d08000011')]}

As mongodb support the Array type, it is really easy to maintain the many to many relationship.

Btw, if you use

references_many :name, :stored_as => :array

before, you will receive a runtime error. You should use

references_and_referenced_in_many :name

instead.

Posted in  mongoid ruby


Fork me on GitHub