Ruby on Rails: Databases. Rails Database Familiar Table Concept Naming convention – lower case, plural (i.e. tweets) How to Access (find), Update, Delete.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Advertisements

Session 2Introduction to Database Technology Data Types and Table Creation.
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.
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II.
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.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
CS 142 Lecture Notes: Rails ActiveRecordSlide 1 Model for Student Table SELECT * FROM students; | id | name.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
A complete ror application. Some easy tutorials This uses oracle but it is easy to replace that with mysql:
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.
Using ERWin to model your data Supplied by Computer Associates as part of AllFusion.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
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:
CSCI 6962: Server-side Design and Programming
1 Dr Alexiei Dingli Web Science Stream Models, Views and Controllers.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Databases and LINQ Visual Basic 2010 How to Program 1.
Chapter 15: Using LINQ to Access Data in C# Programs.
MVC & ActiveRecord by Christian Mohr & Mohamed Souiai.
1 Dr Alexiei Dingli Web Science Stream Introducing Rails.
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.
Lecture 11 Rails Topics SaaSSaaS Readings: SaaS book Ch February CSCE 740 Software Engineering.
Ruby on Rails CSE 190M, Spring 2009 Week 6. Overview How to use a database Demo creating a blog application on Rails Explain how the application works.
© Copyright IBM Corporation 2007 AP/Americas April 15-18, 2007 Anaheim, California Introduction to RubyOnRails - a J2EE replacement? Russell Scheerer –
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Associations INFO 2310: Topics in Web Design and Programming.
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.
Damian Tamayo Tutorial DTM Data Generator Fall 2008 CIS 764.
Testing Carol Wolf Computer Science. Testing built into Rails  Rails comes with three databases.  development  test  production  The test database.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
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.
Database Basics BCIS 3680 Enterprise Programming.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
Relational Databases: Basic Concepts BCHB Lecture 21 By Edwards & Li Slides:
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Introduction to Ruby&Rails Yuri Veremeyenko Monica Verma.
Lecture Set 2 Part A: Creating an Application with Visual Studio – Solutions, Projects, Files 8/10/ :35 PM.
Introduction to information systems RUBY ON RAILS dr inż. Tomasz Pieciukiewicz.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
Migrations Carol Wolf CS 396X. ISBNTitleAuthorImage EmmaAustenemma.jpg Oliver TwistDickenstwist.jpg HamletShakespearehamlet.jpg.
Oracle Business Intelligence Foundation – Testing and Deploying OBI Repository.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
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.
Introduction to Database Programming with Python Gary Stewart
1 Adding a Model. We have created an MVC web app project Added a controller class. Added a view class. Next we will add some classes for managing movies.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
ASP.NET Programming with C# and SQL Server First Edition
Insert, Update and the rest…
SQL – Application Persistence Design Patterns
Ruby on Rails Model of MVC
Agile Web Development with Ruby and Rails
Model for Student Table
CS3220 Web and Internet Programming SQL and MySQL
Model for Student Table
CS3220 Web and Internet Programming SQL and MySQL
SQL – Application Persistence Design Patterns
Presentation transcript:

Ruby on Rails: Databases

Rails Database Familiar Table Concept Naming convention – lower case, plural (i.e. tweets) How to Access (find), Update, Delete (destroy), Add (create)

Rails –connection to a database By default uses SQLite3 (a file based solution) You can modify to connect to other databases (Oracle, MySQL) --- usually you edit the config/database.yml file to point to correct database and connection properties # config/database.yml # SQLite version 3.x # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3 production: <<: *default database: db/production.sqlite3

This lecture and our class….. Concentrates not on setting up a database and connecting to it via rails. But, rather once you have done that how to use it (to find entries, to add, delete and update…sometimes called CRUD = create,read, update, delete). NOTE: we will use default SQLite3 (filebased) that you can simply install as a gem (via > gem install sqlite3) on the command line AGAIN: to connect to a different database you edit the config/database.yml file to point to your database.

Rails Database – Read Access (find) Name tables = lowercase, plural (tweets) Access table via Models (discussed in other lecture) = uppercase, singular (Tweet.find(*) )

Accessing database Retrieve item using find(*) method t = Tweet.find(3) #get the entry with id 3 NOW t = hash {id: 3, status: “I just ate…”, zombie:“Jim” }

Accessing database with “.” syntax - preferred Have t = Tweet.find(3) Can treat as saw before like a Hash, or use “.” syntax (more common) as in database entry t.status VERSUS t[:status]

Database Access -more Tweet.find(2) => gets entry with id=2 Tweet.find(3,4,5) => gets ENTRIES with ids =3,4,5 Tweet.first Tweet.last => gets First and last entry Tweet.all => gets all the entries Tweet.count =>number of entries Tweet.order(:zombie) => returns ORDER BY alphanumeric data in zombie column Tweet.limit(90) => limits to 90 entries returned Tweet.where(zombie: “ash”) => WHERE zombie = “ash”

Database Access –Where conditional clause and ordering results Tweet.where(zombie: “ash”).order(:status).limit(10) MEANS IN SQL WHERE zombie = “ash” AND ORDERBY status AND limit =10

Rails Database: Create New Entry t = Tweet.new t.status = “I < 3 brains.” t.save GENERALLY, T= TableName.new t.key = value t.save NOTE: you do not have to specify the id that is automatically determined by Rails

Rails Database: Create New Entry – ALTERNATIVE 1 t = Tweet.new( status : “I < 3 brains.” zombie: “Jim”) t.save GENERALLY, t= TableName.new( key1: value1, key2: value2) t.save NOTE: you do not have to specify the id that is automatically determined by Rails

Rails Database: Create New Entry – ALTERNATIVE 2 t = Tweet.create( status : “I < 3 brains.”, zombie: “Jim”) GENERALLY, t= TableName.create( key1: value1, key2: value2) NOTE: you do not have to specify the id that is automatically determined by Rails ALSO: you do not have to execute a save

Rails Database: Update Entry First, retrieve it, then update values t = Tweet.find(3) t.zombie = “EyeEater” t.save GENERALLY, t = TableName.find(x) t.key = “new value” t.save

Rails Database: Update Entry – ALTERNATIVE 1 First, retrieve it, then update values using attributes and passing a hash to set all of them t = Tweet.find(3) t.attributes = { status : “Can I eat your Eyes?”, zombie : “EyeEater” } t.save GENERALLY, t = TableName.find(x) t.attributes = { key1: “value 1”, key2: “value 2” } t.save

Rails Database: Update Entry – ALTERNATIVE 2 First, retrieve it, then use update method t = Tweet.find(3) t.update(status : “Can I eat your Eyes?”, zombie : “EyeEater” ) GENERALLY, t = TableName.find(x) t.update(key1: “value 1”, key2: “value 2”) NOTE: you do not have to execute a save

Rails Database: Delete an entry Find Entry, then call destroy method t = Tweet.find(2) t.destroy

CS 142 Lecture Notes: Rails ActiveRecord Slide 17 Migration: Create New Table SUPPOSE you have created a model called Student it will create the following file for you containing the class CreateStudents. Edit it as shown in red to add code to create the corresponding database 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 idnamebirthgpagrad Primary keystringdatefloatint

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

Migration Utilities rails generate model students (NOTE: on RubyMine IDE is : Tools-> Run Rails Generator -> model and then give the name students as the Model name) rails generate migration load_data => db/migrate/load_data.rb rake db:migrate rake db:migrate VERSION= rake db:reset rake db:migrate:reset