Slide 1

Slide 1 text

Nola On Rails Session 4 Databases && Models

Slide 2

Slide 2 text

Retrospective ● Rails keeps important application logic files in app/, config/, and lib/ ● Logs are stored in log/ ● Things directly accessible to the web are in public/ ● The testing directories are spec/, test/, or features/ ● The database stuff is stored in db/

Slide 3

Slide 3 text

What are Databases? ● A database is a collection of data, usually in some specific format ● Databases can be really complex ● But there are simple databases too ● Common databases include: Document, Key/Value, and Relational

Slide 4

Slide 4 text

What is a Model? ● A model is a representation of some concept ● Most of the time this concept is stored in the database ● Think of a Model as a blueprint for data ○ The blueprint shows how the real thing works ○ It shows what the end product will look like ○ It also sometimes shows what is allowed, and what isn't ● Another good analogy is the pilot dashboard on an airplane

Slide 5

Slide 5 text

What is an ODM or ORM? ● ODM (or ORM) stands for Object Data (or Record) Mapper ● Simply it's a library that knows how to take an object in your process and map it to a record in the database, or vise versa ● They exist to make database problems easier ● You write a model, the ODM handles how to save, update, destroy, or create that record ● We use Mongoid, but Rails uses ActiveRecord

Slide 6

Slide 6 text

Other ODMs, ORMs, and databases ● ActiveRecord: Rails staple, for SQL databases (PostgreSQL, MySQL, SQLite, SQLServer, ...) ● Sequel: Underdog, covers more SQL databases than AR ● DataMapper: More modular than ActiveRecord, but harder to use ● ToyStore: For smaller database uses, but still good ● And many more

Slide 7

Slide 7 text

Starting up the database (Mac) ● Open up your terminal ● Open a second tab ● Run brew info mongodb ● Copy the line under "Or start it manually:" ● Paste into the terminal prompt and run it ● You'll see what's called a daemon, or a running process that doesn't take input

Slide 8

Slide 8 text

Starting up the databases (windows) ● Find where you installed MongoDB ● Open mongod.exe ● DO NOT CLOSE THE TERMINAL :) ● You should see the daemon waiting

Slide 9

Slide 9 text

Setting up Mongoid ● Go to your rails directory ● Make sure RVM is loaded (rvm current) ● You should see ruby-1.9.3-p0 ● Open up the Gemfile in your editor ● Below gem 'rails', '3.2.2' write gem 'mongoid', '~> 2' ● Save and go to your terminal (the first tab) ● Run bundle install ● Then run rails generate ● Follow the instructions at the top and find the one relating to mongoid

Slide 10

Slide 10 text

What is YAML? ● You might notice a new file: config/mongoid.yml ● YAML stands for: Yet Another Markup Language ● Markup languages are syntax for data structures around text. ● HTML is a Markup Language, so is XML ● YAML is pretty easy to understand, which is why it's commonly used in Ruby as a settings file ● YAML files end in .yaml or .yml

Slide 11

Slide 11 text

Building our first Model: Story ● Make sure you're in the rails root, and using the right RVM ● Run rails generate model ● These are the different options you can pass to the model generator ● The model generator is not required, but great for new people ● Stories should have a title and body ● Lets also give them timestamps ● Figure out how to from the help banner

Slide 12

Slide 12 text

The models in Rails ● All models in rails are stored in app/models/ ● Open the Story model in your editor ● Most of this code is specific to Mongoid ● We'll talk about classes later, but this is a class ● The fields are the fields on the record ● Each field has a type, default is String ● We're not going to do anything here for now, but know that it exists

Slide 13

Slide 13 text

Creating Your First Record ● While in Rails.root run rails console ● This is a shell to your Rails application ● We'll be using it to play around with the database ● In the console write Story.new and press Enter ● This is a blank story, ready to be filled up with data ● This time try Story.create title: "The Best Story Ever", body: "Note: Write this!"

Slide 14

Slide 14 text

Finding, Inspecting, and Deleting ● In the database you now have data ● Now you need to be able to do things with that data ● First lets find the story we created ● In the console try story = Story.where title: "The Best Story Ever" ● Now the story is "assigned" to the word story ● Type story into the console and see what happens ● Now let's delete that story: story.delete

Slide 15

Slide 15 text

Collection Of Records ● Most of the time you'll be dealing with a collection of records ● Create a second Story (whatever you like) ● Then in the console write stories = Story.all ● This is a Criteria object, it's a collection of all the rules for your search ● Now lets fetch those stories and sort them: stories.sort_by :title ● [] is ruby syntax for an Array, basically a list

Slide 16

Slide 16 text

Resources ● http://mongoid.org ● http://en.wikipedia.org/wiki/Object- relational_mapping