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/
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
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
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
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
• 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
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
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
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
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
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!"
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
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