mongoドライバーで多対多とかきびしそうなのでo/rマッパーのmongoid3をつかってみた by ruby
mongoドライバーだけで多対多とかさすがにきびしいので
mongoid3っていうo/rマッパーつかってみた
たったこれだけで、多対多表現できて、
find_or_create_byメソッドでなければ作るとか
incメソッドでカウントとかべんりすぎー
# coding: utf-8 require "mongoid" Mongoid.load!("mongoid.yml", :development) class Article include Mongoid::Document field :name, type: String field :title, type: String field :link, type: String field :date, type: Time has_and_belongs_to_many :tags end class Tag include Mongoid::Document field :name, type: String field :count, type: Integer, default: -> {0} has_and_belongs_to_many :articles end article=Article.find_or_create_by(name:"a",title:"b",link:"c") tag= Tag.find_or_create_by(name:"d") tag.inc(:count, 1) tag.articles << article article.tags << tag
mongoid.yml
development: sessions: default: username: xxxxx password: xxxxx database: xxxxx hosts: - xxx.mongolab.com:0000
ドキュメント
Mongoid: Home