Agile Web Development with Ruby and Rails

Slides:



Advertisements
Similar presentations
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Advertisements

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
MySql In Action Step by step method to create your own database.
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Taking ActiveRecord to the Next Level Blythe Dunham
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
© Copyright IBM Corporation 2007 AP/Americas April 15-18, 2007 Anaheim, California Introduction to RubyOnRails - a J2EE replacement? Russell Scheerer –
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Associations INFO 2310: Topics in Web Design and Programming.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
Ruby on Rails: Databases. Rails Database Familiar Table Concept Naming convention – lower case, plural (i.e. tweets) How to Access (find), Update, Delete.
1 Active Records. 2 What’s Active Records? O-R Mapping layer To make database access almost a non-issue Relies heavily on convention over configuration.
Chapter 15 © 2013 by Pearson Overview of Rails - Rails is a development framework for Web-based applications - Based on MVC architecture for applications.
1 Migration. 2 What’s Migration? Migration –Isolates database differences Allows you to write schema updates without worries about differences –Helps.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which.
Mr C Johnston ICT Teacher
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Adding Data to a Database Table Carol Wolf Computer Science.
Ruby on Rails & Databases. Active Record Active Record in Rails CRUD & Other Stuff Mapping Cardinalities Migrations Demo.
Migrations Carol Wolf CS 396X. ISBNTitleAuthorImage EmmaAustenemma.jpg Oliver TwistDickenstwist.jpg HamletShakespearehamlet.jpg.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Advanced Migration By Aye Mon Tun.  To change database schema in consistent and easy way  In ruby code Migration? 11/25/2013 2Web Application Engineering.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Data Modeling.
Creating a database table
CS320 Web and Internet Programming SQL and MySQL
Insert, Update and the rest…
Practical Office 2007 Chapter 10
Basic Database Concepts
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
MIS2502: Data Analytics SQL – Putting Information Into a Database
Chapter 2: Relational Model
Data Definition and Data Types
Relational Algebra Chapter 4, Part A
Ruby on Rails Model of MVC
MIS2502: Data Analytics SQL – Putting Information Into a Database
Translation of ER-diagram into Relational Schema
Database Models Relational Model
Ruby on Rails by Manik Juneja
ORACLE SQL Developer & SQLPLUS Statements
STRUCTURED QUERY LANGUAGE
Creating and Modifying Queries
Model for Student Table
MIS2502: Data Analytics SQL – Putting Information Into a Database
SQL OVERVIEW DEFINING A SCHEMA
MIS2502: Data Analytics SQL – Putting Information Into a Database
Oracle Data Definition Language (DDL)
MIS2502: Data Analytics SQL – Putting Information Into a Database
MIS2502: Data Analytics SQL – Putting Information Into a Database
Advanced Database Concepts: Reports & Views
Model for Student Table
MySQL Database System Installation Overview SQL summary
ICT Database Lesson 2 Designing a Database.
Instructor: Samia arshad
MySQL Database System Installation Overview SQL summary
ESRM 250/CFR 520 Autumn 2009 Phil Hurvitz
SQL – Application Persistence Design Patterns
SQL (Structured Query Language)
Presentation transcript:

Agile Web Development with Ruby and Rails Prof. Paul Krause Lecture 7 Migrations and Active Record

Objectives for today A Look at Migrations From Migrations to Active Record Rails’ Object-Relation Mapping More on Rails Controllers

20091030111023_add_price_to_product class AddPriceToProduct < ActiveRecord::Migration def self.up add_column :products, :price, :decimal end def self.down remove_column :products, :price

20091030111023_add_price_to_product class AddPriceToProduct < ActiveRecord::Migration def self.up add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0 end def self.down remove_column :products, :price

Two ways of Generating Migrations Create a Model E.g. generating a model called Discount will also generate a migration: dddddddddddddd_create_discounts.rb Generate a migration on its own E.g. Generate/Migration add_price_column You then need to ask Rails to run a “Rake” task to migrate to the new version

Anatomy of a migration class AddPriceToProduct < ActiveRecord::Migration def self.up add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0 end

Anatomy of a migration class AddPriceToProduct < ActiveRecord::Migration def self.up add_column :products, :price, :decimal, :precision => 8, :scale => 2, :default => 0 end def self.down remove_column :products, :price

Column Types add_column :orders, :e-mail, :string The third parameter specifies the type of the database column. But databases typically don’t have :string as a type? Rails insulates you from the underlying database by using “logical types” E.g. :string type will generate a column of type varchar(255) in MySQL

Column Options :null => true or false :limit => size :default => value N.B. the value is determined at the time of application of the migration: add_column :orders, :placed_at, :datetime, :default => Time.now decimal columns also support the options :precision and :scale

Summary of migration types Create tables Add columns Rename columns Change the type, or options, of columns Rename tables

Warning about renaming tables If you rename a table (e.g. order_histories -> order_notes), you will also need to rename the associated model (e.g. OrderHistory -> OrderNote) Now, suppose you wish to drop the database, and then reapply all migrations? Rerunning the migration to create the table order_histories will generate an exception, as the associated model has been renamed! There are ways around this, but be aware that Rails migrations are not foolproof...

Primary Keys You will not normally explicitly define a primary key Rails assumes: every table has a numeric (integer) primary key (called id) Rails ensures the value of this column is unique for each row added to the database You can change the name of the primary key: create_table :tickets, :primary_key => :number do |t| .. But it is not a good idea to change the type from an integer

Primary Keys You will not normally explicitly define a primary key Rails assumes: every table has a numeric (integer) primary key (called id) Rails ensures the value of this column is unique for each row added to the database You can change the name of the primary key: create_table :tickets, :primary_key => :number do |t| .. But it is not a good idea to change the type from an integer

Tables with no Primary Key The most common case for this is a join table Need to explicitly tell Rails not to generate a primary key: create_table :authors_books, :id => false do |t| t.integer :author_id, :null => false t.integer :book_id, :null => false end

From Migrations to Active Record

Active Record This is Rails’ Object-Relational mapping: Tables map to classes; Rows map to objects; Columns map to object attributes. The “Rails way” is to use sensible defaults to minimise the configuration needed

Simple Example that Does a Lot... ActiveRecord::Base.establish_connection(:adapter => “jdbcmysql”, :database => “development” class Order < ActiveRecord::Base end end order = Order.find(1) order.name = “Paul Krause” order.save Connect to the database Wrap the table of Orders in the database Find the order with a specific ID Change the purchaser’s name Save the result back in the database

More on the slides from Sun Microsystems ...