MVC and the model (Fri Sep 29, lecture 11)

Homework due for today

Legend: : Participation | : Early | : PDF | : Portfolio | : Zipped

  1. Schema Practice: Do Schema Design Exercises. Deliverable: Submit a pdf with a schema for each of the assignments, using the exact format suggested.
  2. RORT: Read Ruby on Rails Tutorial ([RORT]) Chapter 4, “Rails Flavored Ruby”. This is an easier chapter, reviewing a variety of Ruby topics that are extended in Rails. This is possible by the way because Ruby as a programming language is very extensible, and Rails takes advantage of this. It has not been unusual to notice features invented by the Rails team which over time get moved into Ruby core. This chapter is an excellent review of Ruby as well. As usual I really encourage and ask you to go through the code samples and actually type them in. You will see that it really helps your understanding and will assure your success in this course! Use your resources: the TAs, the class mailing list, stack overflow, google and push through to success! When you are done, go to Latte and answer these warmup questions:

    • Give me your explanation of what a symbol is in Ruby. Write a very brief few lines of Ruby to illustrate.
    • What is meant by the idea that Ruby classes can be “opened up” or “reopened”?
    • What is the difference between ‘require’ and ‘require_relative’ in Ruby?
    • Are there one or two things you are still confused by? If not, what one or two things were the most memorable about your reading? Deliverable: Do the warmup in Latte and submit by 8am on the day of class.
  3. Projects: You should be working or completed steps GIT, DOCUMENTATION, PLANSKEL, SHEMASKEL, BUILDSKEL, STORYSDV, PAPERSDV, SCHEMASDV.
  4. Rails PA 1: Continue working on PA Rails 1

Interesting (Optional) reading

Interesting article about Design Constraints: Why Saying “No” is so Effective

Model-View-Controller Review
  • Reminder:
    • Basic Client <-> Server structure
    • MVC is a general pattern
    • Here we are applying it to a web (http) server
  • The Models: contains the application ‘state’
    • Think of it as what you want preserved if your app (i.e. the server in our case) has to be restarted
    • Think of your user’s reasonable expectations
    • Discussion
  • The Controllers: master control switch
    • Sees requests from the outside world
    • Asks the models for data, sometimes updates the data
    • Asks the views for new pages or results
    • Responds to the request
    • (Note: this parallels the HTTP protocol exactly!)
  • The Views
    • Use data (given by the controller)
    • To create and assemble responses to be sent
    • Responses are html, but may be other formats too

Models and the Database

  • “If you don’t know the schema of your app, you don’t know your app” - Said one of our outside reviewers over lunch today
  • ORM means we have to keep the database and the Objects “in sync”
  • What happens if in release 2 I have to modify the database?


  • When you discover you need a new column or table, you can modify the database
  • E.g.break the “name” field into a “first name” and “last name”
  • Data Migration
    • How do I move over all the user records that were already there?
  • Logic Migration
    • What has to change in the server code to accomodate this change?
  • Does this mean that migration is bad?
    • It’s not good nor bad: it’s unavoidable
    • It’s much more painful when you are ‘in production’

ActiveRecord DB Mechanics

  • ActiveRecord
    • Library from Ruby on Rails which is the connection between the rails application and the underlying database
    • For advanced designs, there are options other than Activerecord
    • ActiveRecord is also often used by itself, especially in Sinatra
  • Naming
    • Model.rb file is named singular, e.g. User
    • Corresponding db table is plural and lower case, e.g. users
  • Correspondences
    • Each instance of the class (e.g. User) corresponds to a single record in the dabase
    • When a query returns a bunch of records, you get a collection of instances of the class
    • (this is classic Active Record model)
  • Attributes
    • Rails will automagically create the methods to access each field of the record in the database
  • Different database ‘servers’
    • Sqllite - local database, no need for a server
    • MySql - Standard industrial strength database, free
    • Postgress - Becoming very popular with Rails community
  • Datatypes supported by ActiveRecord
  • Queries and other ways to access the database
  • Validations
  • Tools and notes

Live coding

Look at next class