Model for Student Table

Slides:



Advertisements
Similar presentations
CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:
Advertisements

Query Methods (SQL). What is SQL A programming language for databases. SQL (structured Query Language) It allows you add, edit, delete and run queries.
Trestle Generator Industrial-strength scaffolding for Ruby on Rails web application development.
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.
1 JRuby on Rails GAO ANG Sun Functional Campus Ambassador 1.
CS 142 Lecture Notes: FormsSlide 1 Simple Form Value1: Value2:
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
CS 142 Lecture Notes: Relational DatabasesSlide 1 Relation (Table) namebirthgpagrad Anderson Jones Hernandez
CS 142 Lecture Notes: FormsSlide 1 Simple Form Value1: Value2:
Ruby on Rails Creating a Rails Application Carol E Wolf CS396X.
Ruby on Rails: An Introduction JA-SIG Summer Conference 2007 Michael Irion The University of Tulsa.
Rails and Grails. To get started Make sure you have java installed You can get the sdk and jre at:
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
UC Berkeley Hello Rails. Review: MVC Goal: separate organization of data (model) from UI & presentation (view) by introducing controller –mediates user.
Taking ActiveRecord to the Next Level Blythe Dunham
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
School of Computing and Information Systems CS 371 Web Application Programming CakePHP Server-side Framework.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
IST 210: ORGANIZATION OF DATA Chapter 1. Getting Started IST210 1.
© Copyright IBM Corporation 2007 AP/Americas April 15-18, 2007 Anaheim, California Introduction to RubyOnRails - a J2EE replacement? Russell Scheerer –
CS 142 Lecture Notes: Relational DatabasesSlide 1 Relation (Table) namebirthgpagrad Anderson Jones Hernandez
Associations INFO 2310: Topics in Web Design and Programming.
Chapter 15 © 2009 by Addison Wesley Longman, Inc Overview of Rails - Rails is a development framework for Web-based applications - Rails is written.
Photo Gallery 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.
Web Application Development Technology เทคโนโลยีสำหรับการพัฒนาโปรแกรม ประยุกต์เว็บ Suntorn Witosurapot Phone: or
1 Dr Alexiei Dingli Web Science Stream Web We’ll implement a voting mechanism Using AJAX Web 2.0.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
RUBY ON RAILS (RoR) Ishwor Khadka. Why Ruby on Rails?
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.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:
Ruby on Rails & Databases. Active Record Active Record in Rails CRUD & Other Stuff Mapping Cardinalities Migrations Demo.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Introduction to information systems RUBY ON RAILS dr inż. Tomasz Pieciukiewicz.
Migrations Carol Wolf CS 396X. ISBNTitleAuthorImage EmmaAustenemma.jpg Oliver TwistDickenstwist.jpg HamletShakespearehamlet.jpg.
Chapter Five Objectives Insert Data into tables Create Query files to insert data into tables Make changes to the data in the tables Extract data from.
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.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
Chapter 1. Getting Started IST 210: Organization of Data IST2101.
Introduction to Database Programming with Python Gary Stewart
SEG4110 – Advanced Software Design and Reengineering TOPIC N Ruby on Rails.
Mid-Hudson Valley Linux Users Group Ruby on Rails Web Development is Fun Again MHVLUG Meeting Jan 7 th 2009 Sean Dague sean.dague.net.
1 Database Systems Introduction to Microsoft Access Part 1.
Data Modeling.
Insert, Update and the rest…
SQL in Oracle.
Relation (Table) Row/Tuple/Record Column/Attribute/Field name birth
Ruby on Rails Model of MVC
Relation (Table) Row/Tuple/Record Column/Attribute/Field name birth
Discrete Structures for Computer Science
Agile Web Development with Ruby and Rails
עיבוד תנועות בסביבת SQL Transaction Processing
Chapter 2 Modeling Data in the Organization
CS 142 Lecture Notes: Relational Databases
Painted Shadows - Not Allowed
Model for Student Table
ASP.NET Relationships between tables
A Very Brief Introduction to Relational Databases
Painted Shadows - Not Allowed
SQL (Structured Query Language)
Why We Need Car Parking Systems - Wohr Parking Systems
Database 2.
Types of Stack Parking Systems Offered by Wohr Parking Systems
Add Title.
Presentation transcript:

Model for Student Table SELECT * FROM students; +----+-----------+------------+------+------+ | id | name | birth | gpa | grad | | 1 | Anderson | 1987-10-22 | 3.9 | 2009 | | 2 | Jones | 1990-04-16 | 2.4 | 2012 | | 3 | Hernandez | 1989-08-12 | 3.1 | 2011 | | 4 | Chen | 1990-02-04 | 3.2 | 2011 | Rails model class (app/models/student.rb): class Student < ActiveRecord::Base End Command to create this class: rails generate model student CS 142 Lecture Notes: Rails ActiveRecord

CS 142 Lecture Notes: Rails ActiveRecord Create New Record student = Student.new student.name = "Williams" student.birth = "1989-11-16" student.gpa = 2.8 student.grad = 2012 student.save() CS 142 Lecture Notes: Rails ActiveRecord

CS 142 Lecture Notes: Rails ActiveRecord Read, Update, Delete student = Student.find(187) student = Student.find_by_name("Hernandez") smarties = Student.find(:all, :conditions => "gpa >= 3.0"); smarties = Student.find(:all, :limit => 10, :order => "gpa DESC"); student.gpa = 4.0 student.save() Student.find(187).destroy() CS 142 Lecture Notes: Rails ActiveRecord

Many-to-One Relationships SELECT * FROM students; +----+-----------+------------+------+------+------------+ | id | name | birth | gpa | grad | advisor_id | | 1 | Anderson | 1987-10-22 | 3.9 | 2009 | 2 | | 2 | Jones | 1990-04-16 | 2.4 | 2012 | 1 | | 3 | Hernandez | 1989-08-12 | 3.1 | 2011 | 1 | | 4 | Chen | 1990-02-04 | 3.2 | 2011 | 1 | SELECT * FROM advisors; +----+----------+-----------+ | id | name | title | | 1 | Fujimura | assocprof | | 2 | Bolosky | prof | class Student < ActiveRecord::Base belongs_to :advisor end class Advisor < ActiveRecord::Base has_many :students CS 142 Lecture Notes: Rails ActiveRecord

CS 142 Lecture Notes: Rails ActiveRecord Many-To-One Examples advisor = Advisor.find_by_name("Fujimura") for student in advisor.students do ... end student = Student.find_by_name("Chen") student.advisor = Advisor.find_by_name("Bolosky") student.save CS 142 Lecture Notes: Rails ActiveRecord

Many-to-Many Relationships SELECT * FROM students; +----+-----------+------------+------+------+ | id | name | birth | gpa | grad | | 1 | Anderson | 1987-10-22 | 3.9 | 2009 | | 2 | Jones | 1990-04-16 | 2.4 | 2012 | | 3 | Hernandez | 1989-08-12 | 3.1 | 2011 | | 4 | Chen | 1990-02-04 | 3.2 | 2011 | SELECT * FROM courses; +----+--------+-----------------+-------------+ | id | number | name | quarter | | 1 | CS142 | Web stuff | Winter 2009 | | 2 | ART101 | Finger painting | Fall 2008 | | 3 | ART101 | Finger painting | Winter 2009 | | 4 | PE204 | Mud wrestling | Winter 2009 | SELECT * FROM courses_students; +-----------+------------+ | course_id | student_id | | 1 | 1 | | 3 | 1 | | 4 | 1 | | 1 | 2 | | 2 | 2 | | 1 | 3 | | 2 | 4 | | 4 | 4 | class Student < ActiveRecord::Base has_and_belongs_to_many :courses end class Course < ActiveRecord::Base has_and_belongs_to_many :students CS 142 Lecture Notes: Rails ActiveRecord

Many-To-Many Examples student = Student.find_by_name("Anderson") cs142 = Course.find_by_number("CS142") student.courses << cs142 CS 142 Lecture Notes: Rails ActiveRecord

Migration: Create New Table db/migrate/20090215220309_create_students.rb: class CreateStudents < ActiveRecord::Migration def up create_table :students do |t| t.column :name, :string t.column :birth, :date t.column :gpa, :float t.column :grad, :integer end def down drop_table :students CS 142 Lecture Notes: Rails ActiveRecord

CS 142 Lecture Notes: Rails ActiveRecord Migration: Add Column db/migrate/20101013224357_add_advisor.rb: class AddAdvisor < ActiveRecord::Migration def up add_column :students, :advisor_id, :integer end def down remove_column :students, :advisor_id CS 142 Lecture Notes: Rails ActiveRecord

CS 142 Lecture Notes: Rails ActiveRecord Migration Utilities rails generate migration create_students rails generate model students rake db:migrate rake db:migrate VERSION=20090130180755 rake db:migrate VERSION=0 rake db:migrate:reset CS 142 Lecture Notes: Rails ActiveRecord

CS 140 Lecture Notes: File Systems