Skip to content

Instantly share code, notes, and snippets.

@kevin-chau
Last active February 22, 2017 10:40
Show Gist options
  • Save kevin-chau/d772eb7d8ccc92f0e42ab79e5c899cf8 to your computer and use it in GitHub Desktop.
Save kevin-chau/d772eb7d8ccc92f0e42ab79e5c899cf8 to your computer and use it in GitHub Desktop.
MongoDB tutorial for RAInvestor

How to set up a Houses database for RAInvestor using Mongoid and Rails in 5 easy steps.

Start the MongoDB server:

$ brew services start mongodb

Generate Rails scaffolding for houses:

$ rails generate scaffold house name street city state 

This creates the following model:

$ cat app/models/house.rb

class House
	 include Mongoid::Document
	field :name, type: String
	field :street, type: String
	field :city, type: String
	field :state, type: String
end

Start the rails server:

$ rails s

Go To:

http://localhost:3000/houses

Click New House, and fill in the information.

Now we will use mongoDB shell to view our database (name in mongoid.yml):

$ mongo ra_investor_development

> show collections
houses
> db.houses.find()
{ "_id" : ObjectId("58a44524daba3773cc012681"), "name" : "The White House", "street" : "President Street", "city" : "Washington", "state" : "District of Columbia" }

Reference

http://kerrizor.com/blog/2014/04/02/quick-intro-to-mongodb-in-rails

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment