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

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.
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.
CS 142 Lecture Notes: Rails Controllers and ViewsSlide 1 Simple Rails Template
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.
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:
Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.
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.
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
Session 8: Databases Teaching Computing to GCSE Level with Python.
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.
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.
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.
Create, Update and Delete Carol Wolf Computer Science.
Assignment 1 Uploaded to course website Due next Tuesday, Sep 1, at 11:59pm.
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.
The Controller Carol Wolf Computer Science. Rails generate commands  Using the generate command, you can create a number of useful objects.  Rails:
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.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 7: Entity-Relationship.
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.
Data Modeling.
COP5725 Database Management ER DIAGRAM AND RELATIONAL DATA MODEL
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
Chapter 2 Modeling Data in the Organization
CS 142 Lecture Notes: Relational Databases
CS3220 Web and Internet Programming SQL and MySQL
Model for Student Table
ASP.NET Relationships between tables
A Very Brief Introduction to Relational Databases
CMPE/SE 131 Software Engineering February 7 Class Meeting
CS3220 Web and Internet Programming SQL and MySQL
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 students = Student.all() student = Student.find(187) student = Student.find_by(name: "Hernandez") student = Student.find_by_name("Hernandez") smarties = Student.where("gpa >= 3.0") smarties = Student.order("gpa DESC").limit(10) student = Student.find(187) student.gpa = 4.0 student.save() Student.find(187).destroy()

CS 142 Lecture Notes: Rails ActiveRecordSlide 4 Simple Example app/controllers/students_controller.rb: class StudentsController < ApplicationController... def = Student.all end end

CS 142 Lecture Notes: Rails ActiveRecordSlide 5 Simple Example, cont’d app/views/students/index.html.erb: Current Students Name Date of Birth GPA Graduation Year "> <%= link_to(student.name, action: :edit, id: student.id) %>

CS 142 Lecture Notes: Rails ActiveRecordSlide 6 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 7 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 Additional methods defined by Rails

CS 142 Lecture Notes: Rails ActiveRecordSlide 8 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 9 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 Additional methods defined by Rails

CS 142 Lecture Notes: Rails ActiveRecordSlide 10 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 11 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 12 Migration Utilities rails generate migration create_students => db/migrate/ _create_students.rb rails generate model students rake db:migrate rake db:migrate VERSION= rake db:reset rake db:migrate:reset

CS 140 Lecture Notes: File SystemsSlide 13