9
Rails with MongoDB-2 Eugene Park [email protected]

Rails with MongoDB - RORLab 47th

Embed Size (px)

DESCRIPTION

Rails with MongoDB - RORLab 47th Oct 8, 2013

Citation preview

Page 1: Rails with MongoDB - RORLab 47th

Rails with MongoDB-2

Eugene [email protected]

Page 2: Rails with MongoDB - RORLab 47th

Setup Ruby On Rails on Ubuntu 13.04

• Installing Ruby with rvm

• Installing Rails

$ sudo apt-get update $ Sudo apt-get install curl$ curl -L https://get.rvm.io | bash -s stable $ source ~/.rvm/scripts/rvm$ rvm requriements$ Sudo apt-get ... $ rvm install 2.0.0 $ rvm use 2.0.0 --default $ ruby -v

$ gem install rails

Page 3: Rails with MongoDB - RORLab 47th

Installing MongoDB on Ubuntu 13.04

• Configure Package Management System (APT)

• Install Packages

• configration

$ sudo apt-key adv --keyserver hkp://keyserver.ubun-tu.com:80 --recv 7F0CEB10

$ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

$ sudo apt-get update

$ sudo apt-get install mongodb-10gen

$ whereis mongodb.conf

Page 4: Rails with MongoDB - RORLab 47th

Installing mongoid• installing Mongoid is with bundler

• Configuration

$ vi Gemfilegem 'mongoid', github: 'mongoid/mongoid‘gem 'bson', github: 'mongodb/bson-ruby'gem 'execjs‘gem 'therubyracer‘

$ bundle update

$ Rails g mongoid:config$ vi myapp/config/mongoid.yml

Page 5: Rails with MongoDB - RORLab 47th

MongoDB SQL

Create Insert (Save) Insert

Read Find Select

Update Update (Upsert) Update

Delete Remove Delete

CRUD & the Mon-goshell

Page 6: Rails with MongoDB - RORLab 47th

book = {_id : 10203040,title : “Rails wth MongoDB Guide”,auther : “Eugene Park”,available : 987,checkout : [ {by : “Eugene”, date : ISOdate(“2013-10-01”)}]

}

db.books.findAndModify({query: {

_id : 10203040,available: { $gt: 0 }},

update: {$inc : { available : -1},$push : { checkout : { by: “Crayon”, data: new Date()}}

} })

Atomic operations

Page 7: Rails with MongoDB - RORLab 47th

Schema Design

• The data your application needs.• How your application will read the data.• How your application will write the data.

Traditional MongoDB

Your application doesn’t matter.

Only about the data.

Only relevant during design.

It’s always about your applica-tion.

Not only about the data, but also how it’s used.

Relevant during the lifetime of your application.

Page 8: Rails with MongoDB - RORLab 47th

Blog data modelposts

idauthortitleBody

comments

idpost_idtag_idauthorbody

posts_tags

idpost_idtag_id

tags

idtext

posts

idtitlebodytags[]comment[{author: …,body: …},{…}]

RDBMS

MongoDB

Page 9: Rails with MongoDB - RORLab 47th

Create blog apps$ rails new blog –-skip-active-record$ cd blog$ vi Gemfile

#add and savegem 'mongoid', github: 'mongoid/mongoid‘gem 'bson', github: 'mongodb/bson-ruby'gem 'execjs‘gem 'therubyracer‘

$ bundle$ rake g mongoid:config$ rails g scaffold post title:string body:text tags:array

... string to array①$ Rails g model comment author:string body:text

... def relationship, routes②$ rails g controller comments

... def create③

... create list, form for comment④