19. “ The philosophy of Mongoid is to provide a
familiar API to Ruby developers who have been
using Active Record or Data Mapper, while
leveraging the power of MongoDB's schemaless
and performant document-based design, dynamic
queries, and atomic modifier operations
”
Durran Jordan, автор Mongoid
35. class Contact class Place
include Mongoid::Document include Mongoid::Document
field :type embeds_many :contacts
field :value end
embedded_in :place
end
place.contacts << Contact.new(:type => "phone", :value => "32-14-90")
place.contacts << Contact.new(:type => "email", :value => "omskps1@rosinter.ru")
place.save
38. class Contact
class Place
include Mongoid::Document
include Mongoid::Document
field :type
embeds_many :contacts
field :value
end
embedded_in :place
end class Email < Contact
end
class Phone < Contact
end
place.contacts << Phone.new(:value => "32-14-90")
place.contacts << Email.new(:value => "omskps1@rosinter.ru")
place.save
58. GridFS
CarrierWave.configure do |config|
config.storage = :grid_fs
config.grid_fs_connection = Mongoid.database
config.grid_fs_host = app_config['domain']
config.grid_fs_access_url = "/gridfs"
end
class PhotoUploader < CarrierWave::Uploader::Base
storage :grid_fs
end
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
59. Локализация
class Person
include Mongoid::Document
field :first_name, :localize => true
field :last_name, :localize => true
end
{ "_id" : 1,
"first_name" : { "ru" : "Иван" },
"last_name" : { "ru" : "Немытченко" }
}
60. Полный фарш
class Person
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Versioning
include Mongoid::Paranoia
field :first_name
field :last_name
key :first_name, :last_name
end
61. Гибридные приложения
class TodoLog class Todo < ActiveRecord::Base
include Mongoid::Document after_create :create_log
field :todo_id, :type => Integer after_update :create_log
field :title, :type => String
field :done, :type => Boolean private
end def create_log
TodoLog.create(
:title => title,
:done => !!done,
:todo_id => id
)
end
end
https://github.com/a2ikm/activerecord-and-mongoid-sample
62. Анти-mongoid
This tool exposes simplicity and power of
“ MongoDB and leverages its differences
”
Алексей Петрушин, автор MongoModel
http://alexeypetrushin.github.com/mongodb_model/index.html