CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; +----+-----------+------------+------+------+ | id | name.

Slides:



Advertisements
Similar presentations
Query Methods (SQL). What is SQL A programming language for databases. SQL (structured Query Language) It allows you add, edit, delete and run queries.
Advertisements

The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Trestle Generator Industrial-strength scaffolding for Ruby on Rails web application development.
Ruby on Rails CSE 190M, Spring 2009 Week 8. Models Up to this point, all of our work has been in our Controllers and View If you have inspected the Models.
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: 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.
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.
Why rails? Carlos Kirkconnell. Google Happiness leads to Productivity Happiness Matters.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
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 –
1 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An = {(x1, x2, x3, …, xn): xi  Ai} R  A1  A2.
CS 142 Lecture Notes: Relational DatabasesSlide 1 Relation (Table) namebirthgpagrad Anderson Jones Hernandez
Associations INFO 2310: Topics in Web Design and Programming.
1 Dr Alexiei Dingli Web Science Stream A ROR Blog.
Ruby on Rails vs ASP.NET MVC Simone Chiaretta Web Architect, Council of the EU Milano, 19 Febbraio 2011 Sandro.
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.
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.
Adding Data to a Database Table Carol Wolf Computer Science.
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.
JPA in Vaadin CSCI 3130 Winter What is JPA?  Java Persistence API  Allows for “easy” storage of Java Objects  Is a type of Object Relational.
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.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
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
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
Agile Web Development with Ruby and Rails
Model for Student Table
עיבוד תנועות בסביבת SQL Transaction Processing
Chapter 2 Modeling Data in the Organization
CS 142 Lecture Notes: Relational Databases
Lecture 24: Creating a Database and More joins
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
Types of Stack Parking Systems Offered by Wohr Parking Systems
Add Title.
Presentation transcript:

CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name | birth | gpa | grad | | 1 | Anderson | | 3.9 | 2009 | | 2 | Jones | | 2.4 | 2012 | | 3 | Hernandez | | 3.1 | 2011 | | 4 | Chen | | 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 ActiveRecordSlide 2 Create New Record student = Student.new student.name = "Williams" student.birth = " " student.gpa = 2.8 student.grad = 2012 student.save()

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

CS 142 Lecture Notes: Rails ActiveRecordSlide 4 Many-to-One Relationships SELECT * FROM students; | id | name | birth | gpa | grad | advisor_id | | 1 | Anderson | | 3.9 | 2009 | 2 | | 2 | Jones | | 2.4 | 2012 | 1 | | 3 | Hernandez | | 3.1 | 2011 | 1 | | 4 | Chen | | 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 end class Student < ActiveRecord::Base belongs_to :advisor end class Advisor < ActiveRecord::Base has_many :students end

CS 142 Lecture Notes: Rails ActiveRecordSlide 5 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 ActiveRecordSlide 6 Many-to-Many Relationships SELECT * FROM students; | id | name | birth | gpa | grad | | 1 | Anderson | | 3.9 | 2009 | | 2 | Jones | | 2.4 | 2012 | | 3 | Hernandez | | 3.1 | 2011 | | 4 | Chen | | 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 end class Student < ActiveRecord::Base has_and_belongs_to_many :courses end class Course < ActiveRecord::Base has_and_belongs_to_many :students end

CS 142 Lecture Notes: Rails ActiveRecordSlide 7 Many-To-Many Examples student = Student.find_by_name("Anderson") for course in student.courses do... end cs142 = Course.find_by_number("CS142") student.courses << cs142

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

CS 142 Lecture Notes: Rails ActiveRecordSlide 9 Migration: Add Column db/migrate/ _add_advisor.rb: class AddAdvisor < ActiveRecord::Migration def change add_column :students, :advisor_id, :integer end

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

CS 140 Lecture Notes: File SystemsSlide 11