Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Databases && Models

Databases && Models

A brief introduction into the world of databases and models

Kurtis Rainbolt-Greene

March 14, 2012
Tweet

More Decks by Kurtis Rainbolt-Greene

Other Decks in Programming

Transcript

  1. 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/
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. Starting up the databases (windows) • Find where you installed

    MongoDB • Open mongod.exe • DO NOT CLOSE THE TERMINAL :) • You should see the daemon waiting
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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!"
  13. 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
  14. 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