activerecord vs mongoid

63
ActiveRecord vs Mongoid Иван Немытченко, 7bits, Омск

Upload: ivan-nemytchenko

Post on 08-May-2015

5.190 views

Category:

Technology


0 download

DESCRIPTION

DevConf 2012 ruby section.

TRANSCRIPT

Page 1: ActiveRecord vs Mongoid

ActiveRecord vs Mongoid

Иван Немытченко, 7bits, Омск

Page 2: ActiveRecord vs Mongoid

PHP → Ruby on Rails →freelance →JazzCloud → Tulp → 7bits

Page 3: ActiveRecord vs Mongoid

2007 - Ruby on Rails

Page 4: ActiveRecord vs Mongoid

2007 - Ruby on Rails

2008 - HAML/SASS

Page 5: ActiveRecord vs Mongoid

2007 - Ruby on Rails

2008 - HAML/SASS

2010 - Coffeescript

Page 6: ActiveRecord vs Mongoid

2007 - Ruby on Rails

2008 - HAML/SASS

2010 - Coffeescript

2010 - Tequila RABL

Page 7: ActiveRecord vs Mongoid

2007 - Ruby on Rails 2008 - HAML/SASS 2010 - Coffeescript 2010 - Tequila → RABL

2011 - Tulp 2012 - MongoDB

Page 8: ActiveRecord vs Mongoid

2007 - Ruby on Rails

2008 - HAML/SASS

2010 - Coffeescript

2010 - Tequila RABL

2012 - ActiveRecord vs Mongoid

Page 9: ActiveRecord vs Mongoid

MongoDB

Page 10: ActiveRecord vs Mongoid

MongoDB is a scalable, high-performance, open

source NoSQL database“

Page 11: ActiveRecord vs Mongoid

НЕТ ТРАНЗАКЦИЙ

Page 12: ActiveRecord vs Mongoid

НЕТ ТРАНЗАКЦИЙ

НЕТ ДЖОЙНОВ

Page 13: ActiveRecord vs Mongoid

Таблицы → Коллекции

Page 14: ActiveRecord vs Mongoid

Таблицы → Коллекции

Строки → Документы

Page 15: ActiveRecord vs Mongoid

{ "_id" : ObjectId( "4fb8dd1324ec7c1344000003" ), "agent" : "Ivan Nemytchenko", "challenge" : "Introduce MongoDB documents", "status" : "accepted" }

Page 16: ActiveRecord vs Mongoid

{ "_id" : ObjectId( "4fb8dd1324ec7c1344000003" ), "agent" : "Ivan Nemytchenko", "missions": [ { "challenge" : "Introduce MongoDB documents", "status" : "done" }, { "challenge" : "Tell the truth about MongoDB", "status" : "considering" } ] }

Page 17: ActiveRecord vs Mongoid

Таблицы → Коллекции

Строки → Документы

Индексы → Индексы

Page 18: ActiveRecord vs Mongoid

MongoidObject Document Mapper

Page 19: ActiveRecord vs Mongoid

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

Page 20: ActiveRecord vs Mongoid

Связи

Page 21: ActiveRecord vs Mongoid

has_many

has_one

belongs_to

Page 22: ActiveRecord vs Mongoid

has_many

has_one

belongs_to

has_and_belongs_to_many

Page 23: ActiveRecord vs Mongoid

has_many

has_one

belongs_to

has_and_belongs_to_many

address:

{organization_ids:[1,2,3]}

organization:

{address_ids:[5,3,1,10]}

Page 24: ActiveRecord vs Mongoid

has_many

has_one

belongs_to

has_and_belongs_to_many

has_many :through

Page 25: ActiveRecord vs Mongoid

has_many

has_one

belongs_to

has_and_belongs_to_many

has_many :through

Page 26: ActiveRecord vs Mongoid

has_many

has_one

belongs_to

has_and_belongs_to_many

has_many :through

embeds_many

embeds_one

embedded_in

Page 27: ActiveRecord vs Mongoid

Lets Practice!

Page 28: ActiveRecord vs Mongoid
Page 29: ActiveRecord vs Mongoid
Page 30: ActiveRecord vs Mongoid
Page 31: ActiveRecord vs Mongoid
Page 32: ActiveRecord vs Mongoid
Page 33: ActiveRecord vs Mongoid
Page 34: ActiveRecord vs Mongoid
Page 35: ActiveRecord vs Mongoid

class Contact include Mongoid::Document field :type field :value embedded_in :placeend

place.contacts << Contact.new(:type => "phone", :value => "32-14-90")place.contacts << Contact.new(:type => "email", :value => "[email protected]")place.save

class Place include Mongoid::Document embeds_many :contactsend

Page 36: ActiveRecord vs Mongoid

{ "_id" : 1, "name" : "Планета Суши" "contacts" : [ { "_id" : 1, "type" : "phone", "value" : "32-14-90" }, { "_id" : 2, "type" : "email", "value" : "[email protected]" } ] }

Page 37: ActiveRecord vs Mongoid

{ "_id" : 1, "name" : "Планета Суши" "contacts" : [ { "_id" : 1, "_type" : "phone", "value" : "32-14-90" }, { "_id" : 2, "_type" : "email", "value" : "[email protected]" } ] }

Page 38: ActiveRecord vs Mongoid

class Contact include Mongoid::Document field :type field :value embedded_in :placeend

place.contacts << Phone.new(:value => "32-14-90")place.contacts << Email.new(:value => "[email protected]")place.save

class Place include Mongoid::Document embeds_many :contactsend

class Email < Contactend

class Phone < Contact end

Page 39: ActiveRecord vs Mongoid
Page 40: ActiveRecord vs Mongoid
Page 41: ActiveRecord vs Mongoid
Page 42: ActiveRecord vs Mongoid

Rating embedded_in :place

Place embeds_many :ratings

RatingType embedded_in :category

Category embeds_many :rating_types

Page 43: ActiveRecord vs Mongoid

Rating embedded_in :place

Place embeds_many :ratings

RatingType embedded_in :category

Category embeds_many :rating_types

Place{ "_id" : 1, "name" : "Планета Суши" "ratings" : [ { "_id":1, "type":"Качество обслуживания", "value":4 }, { "_id":2, "type":"Чистота", "value":5 } ] }

Page 44: ActiveRecord vs Mongoid

Rating embedded_in :place

Place embeds_many :ratings

RatingType embedded_in :category

Category embeds_many :rating_types

Place{ "_id" : 1, "name" : "Планета Суши" "ratings" : [ { "_id":1, "type":"Качество обслуживания", "value":4 }, { "_id":2, "type":"Чистота", "value":5 } ] }

Category{ "_id" : 1, "name" : "Рестораны" "rating_types" : ["Качество обслуживания", "Чистота"] }

Page 45: ActiveRecord vs Mongoid

Place.where(:"ratings.type"=>"Чистота", :"ratings.value.gt"=>3)

Place{ "_id" : 1, "name" : "Планета Суши" "ratings" : [ { "_id":1, "type":"Качество обслуживания", "value":4 }, { "_id":2, "type":"Чистота", "value":5 } ] }

Category{ "_id" : 1, "name" : "Рестораны" "rating_types" : ["Качество обслуживания", "Чистота"] }

Page 46: ActiveRecord vs Mongoid
Page 47: ActiveRecord vs Mongoid
Page 48: ActiveRecord vs Mongoid
Page 49: ActiveRecord vs Mongoid

Property embedded_in :place field :question field :answerPlace embeds_many :properties

Answer embedded_in :question

Question embedded_in :category embeds_many :answers

Category embeds_many :questions

Page 50: ActiveRecord vs Mongoid

>> category.questions << Question.new(:value => 'WiFi', :answers => ['есть', 'нету'])

>> place.properties << Property.new(:question => 'WiFi', :answer => 'есть')

Page 51: ActiveRecord vs Mongoid
Page 52: ActiveRecord vs Mongoid
Page 53: ActiveRecord vs Mongoid
Page 54: ActiveRecord vs Mongoid

Place{ "_id" : 1, "name" : ".." "contacts" : [ { "_type" : "phone", "value" : "32-14-90" }, { "_type" : "email", "value" : "..." }], "ratings" : [ { "type" : "..", "value" : ".." }, { "type" : "..", "value" : ".." }], "properties" : [ { "question" : "..", "answer" : ".." }], "categories" : ["..", ".."], "review_ids" : [12, 34, 56] }

Review{ "_id" : 12, "user_id" : 16, "place_id" : 1, "body" : “..”, "comments" : [ { "body" : "..", "user_id" : 1, "user_name" : ".." }, { "body" : "..", "user_id" : 5, "user_name" : ".."}], "ratings" : [ { "type" : "..", "value" : ".." }, { "type" : "..", "value" : ".." }]}

Page 55: ActiveRecord vs Mongoid

Place

Contacts

Ratings

Properties

Categories →

Category

RatingTypes

Questions

Answers

Review

AuthorSummary

Ratings

Comments

User

Reviews →

Page 56: ActiveRecord vs Mongoid

Соотношение запись/чтение

Жизненный цикл объекта

Так ли важна целостность данных?

To embed or not to embed?

Page 57: ActiveRecord vs Mongoid

Extra Stuff

Page 58: ActiveRecord vs Mongoid

GridFS

gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'

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_fsend

Page 59: ActiveRecord vs Mongoid

class Person include Mongoid::Document

field :first_name, :localize => true field :last_name, :localize => trueend

Локализация

{ "_id" : 1, "first_name" : { "ru" : "Иван" }, "last_name" : { "ru" : "Немытченко" } }

Page 60: ActiveRecord vs Mongoid

Полный фаршclass Person include Mongoid::Document include Mongoid::Timestamps include Mongoid::Versioning include Mongoid::Paranoia

field :first_name field :last_name key :first_name, :last_nameend

Page 61: ActiveRecord vs Mongoid

Гибридные приложения

https://github.com/a2ikm/activerecord-and-mongoid-sample

class TodoLog include Mongoid::Document field :todo_id, :type => Integer field :title, :type => String field :done, :type => Booleanend

class Todo < ActiveRecord::Base after_create :create_log after_update :create_log private def create_log TodoLog.create( :title => title, :done => !!done, :todo_id => id ) endend

Page 62: ActiveRecord vs Mongoid

Анти-mongoid

http://alexeypetrushin.github.com/mongodb_model/index.html

This tool exposes simplicity and power of

MongoDB and leverages its differences“

”Алексей Петрушин, автор MongoModel

Page 63: ActiveRecord vs Mongoid

Спасибо. Давайте поговорим.

@inem

[email protected]