LoginSignup
27
28

More than 5 years have passed since last update.

Rails4でMongoDBを使う

Last updated at Posted at 2014-01-31

プロジェクト作成

$ rails new mongo_test -O --skip-bundle

※-OオプションでActiveRecordをスキップ

gemの追加

gem 'mongoid', '4.0.0.alpha2'
gem 'bson_ext'
$ bundle install

mongoidの設定ファイル作成

$ rails generate mongoid:config

これで、config/mongoid.ymlが作成される。

とりあえず、localで使う分にはデフォルトでだいたいOK。

ここまでで、localでMongoDBは使えるはず。

Scaffoldで動作確認

$ rails g scaffold User name email

あとは、http://localhost:3000/usersにアクセスして、userを作ってみる。

MongoDB側で確認

# mongoシェルの起動
$ mongo

# データベース一覧
> show dbs;
local xxxxGB
mongo_test_development xxxxGB

> use mongo_test_development;

# コレクション一覧
> show collections;
system.indexes
users

# ドキュメント一覧
> db.users.find();
{"_id" : ObjectId("xxxxxxxxxxxxxxxx"), "name" : "hoge", "email" : "hoge@example.com"}

参照

酒と泪とRubyとRailsと - Herokuの超優良アドオン MongoLabを Rails 4.0 X Ruby 2.1環境で使う!
なるひこの Linux Printing お勉強日記 - Ruby初心者がRuby on Rails + Mongoidを試してみた
そういうことだったんですね - DB - MongoDB(3) mongoid

27
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
28