How To Create A Model In Ruby 2.5.2.2 (Orm)



Similar documents
Lecture 3 ActiveRecord Rails persistence layer

Building Dynamic Web 2.0 Websites with Ruby on Rails

Deep in the CRUD LEVEL 1

2010 Users Symposium Berlin

Ruby on Rails. Computerlabor

1. Was ist das? II. Wie funktioniert das? III. Wo funktioniert das nicht?

Rails 4 Quickly. Bala Paranj.

Content Management System (Dokument- og Sagsstyringssystem)

Ruby & Ruby on Rails for arkitekter. Kim Harding Christensen khc@kimharding.com

Ruby on Rails is a web application framework written in Ruby, a dynamically typed programming language The amazing productivity claims of Rails is

Building and Deploying Web Scale Social Networking Applications Using Ruby on Rails and Oracle. Kuassi Mensah Group Product Manager

This tutorial has been designed for beginners who would like to use the Ruby framework for developing database-backed web applications.

Ruby On Rails A Cheatsheet. Ruby On Rails Commands

Comparing Dynamic and Static Language Approaches to Web Frameworks

Diese Liste wird präsentiert von. Netheweb.de

Kapitel 2 Unternehmensarchitektur III


Entwicklung von Integrationslösungen aus einem Guss mit AIA Foundation Pack 11g

Search Engines Chapter 2 Architecture Felix Naumann

RESTful Rails Development. translated by Florian Görsdorf and Ed Ruder

Semantic Web. Semantic Web: Resource Description Framework (RDF) cont. Resource Description Framework (RDF) W3C Definition:

BDD with Cucumber and RSpec. Marcus Ahnve Valtech

Model-Based, Event-Driven Programming Paradigm for Interactive Web Applications

HAML und SASS. (und COMPASS) markup haiku vs. syntactically awesome stylesheets. Tobias Adam, Jan Krutisch mindmatters GmbH & Co.

Chris van Daele / / QuestBack, Köln. Laravel 5 Framework. The PHP Framework for Web Artisans

Guides.rubyonrails.org

Erste Schritte mit mysql. Der Umgang mit einer relationalen Datenbank

Gleb Arshinov, Alexander Dymo PGCon 2010 PostgreSQL as a secret weapon for high-performance Ruby on Rails applications

Evaluation. Chapter 1: An Overview Of Ruby Rails. Copy. 6) Static Pages Within a Rails Application

Software / FileMaker / Plug-Ins Mailit 6 for FileMaker 10-13

ausführliche Dokumentation, Stand:

Embedded Software Development and Test in 2011 using a mini- HIL approach

Ruby on Rails Tutorial - Bounded Verification of Data Models

VSTO 3.0 In Action mit Outlook Lars Keller netcreate OHG

Stefan Engelberg (IDS Mannheim), Workshop Corpora in Lexical Research, Bucharest, Nov [Folie 1]

Java Server Pages combined with servlets in action. Generals. Java Servlets

AJAX SSL- Wizard Reference

AnyWeb AG

Exchange Synchronization AX 2012

Outline. Lecture 18: Ruby on Rails MVC. Introduction to Rails

Intruduction to Groovy & Grails programming languages beyond Java

Marc Grote. OCSP / CRL Schlüssel Archivierung CA-Konsole Certutil PKI Health

Big Data Vendor Benchmark 2015 A Comparison of Hardware Vendors, Software Vendors and Service Providers

Rainer Stropek software architects gmbh. Entwicklung modularer Anwendungen mit C# und dem Managed Extensibility Framework (MEF)

Agenda. 1. ZAPms Konzept. 2. Benutzer-Kontroller. 3. Laout-Aufbau. 4. Template-Aufbau. 6. Konfiguration. 7. Module.

Microsoft Nano Server «Tuva» Rinon Belegu

Security Vendor Benchmark 2016 A Comparison of Security Vendors and Service Providers

Timebox Planning View der agile Ansatz für die visuelle Planung von System Engineering Projekt Portfolios

TIn 1: Lecture 3: Lernziele. Lecture 3 The Belly of the Architect. Basic internal components of the Pointers and data storage in memory

Programming Autodesk PLM 360 Using REST. Doug Redmond Software Engineer, Autodesk

How To Manage Build And Release With Tfs 2013

Klasse: Z_LDAP_TELEFONBUCH

Information Systems 2

APPLICATION SETUP DOCUMENT

LEARNING AGREEMENT FOR STUDIES

SPICE auf der Überholspur. Vergleich von ISO (TR) und Automotive SPICE

quick documentation Die Parameter der Installation sind in diesem Artikel zu finden:

Web Framework Performance Examples from Django and Rails

SQL 2014 CTP1. Hekaton & CSI Version 2 unter der Lupe. Sascha Götz Karlsruhe, 03. Dezember 2013

How To Teach A Software Engineer

Ruby on Rails Web Mashup Projects

A New Job Opportunity for Unemployed Young People: Data Security Skilled Worker for SMEs

Thomas Rümmler AIT GmbH & Co. KG Christian Schlag AIT GmbH & Co. KG. Central Build and Release Management with TFS

The Learn-Verified Full Stack Web Development Program

2. Installieren des MySQL Workbench (Version ) 3. Unter Database > Manage Connection folgende Werte eintragen

Open Text Social Media. Actual Status, Strategy and Roadmap

Leitfaden für die Antragstellung zur Förderung einer nationalen Biomaterialbankeninitiative

Exploring Ruby and Java Interoperability for Building Converged Web and SIP Applications

Produktfamilienentwicklung

Installation Sophos Virenscanner auf Friedolins Linux Servern

Social Media: How to do it right in Denmark and Germany

J2EE-Application Server

dfgproposal.cls it reflects the current DFG template valid from October 2011.

1Copyright 2013, Oracle and/or its affiliates. All rights reserved.

Mit einem Auge auf den mathema/schen Horizont: Was der Lehrer braucht für die Zukun= seiner Schüler

Mul$media im Netz (Online Mul$media) Wintersemester 2014/15. Übung 03 (Nebenfach)

Cloud Elements! Events Management BETA! API Version 2.0

Development and Validation of an XML Schema for Automated Environmental Reporting on XML Basis

How To Write A Validator In Java 1.1.1

TBarCode.NET Barcodes in MS SQL Reporting Services

Determination of the intervals of periodic inspections for composite pressure receptacles

Ruby On Rails. James Reynolds

Dominik Helleberg inovex GmbH. Moderne Android Builds mit Gradle

I-Q SCHACHT & KOLLEGEN QUALITÄTSKONSTRUKTION GMBH ISO 26262:2011. Liste der Work Products aus der Norm

Technical Guidelines. for Power Generating Units. Part 7: Operation and maintenance of power plants for renewable energy Category D3 Attachment A:

A Beginner's Guide to Security with Ruby on Rails in BSD. Corey Benninger

17. November Übung 1 mit Swift. Architektur verteilter Anwendungen. Thorsten Kober Head Solutions ios/os X, SemVox GmbH

Transcription:

Gastvortrag Datenbanksysteme: Nils Haldenwang, B.Sc. 1 Institut für Informatik AG Medieninformatik

Models mit ActiveRecord 2

Model: ORM pluralisierter snake_case Tabelle: users Klasse: User id first_name last_name age 1 Nils Haldenwang 25 snake_case 2 Luke Skywalker 42 Idee: Repräsentiere die Tabelle durch eine Klasse und die Zeilen durch einzelne Instanzen.

Migrations 4

Migrations Ruby DSL zur Definition und Manipulation des Datenbankschemas Datenbankunabhängig (in gewissem Rahmen) Inkrementelle Entwicklung der Datenbank Kann mit in die Versionskontrolle eingecheckt werden Automatische Versionierung Gesamtschema in db/schema.rb 5

Beispiel: Tabelle erstellen class CreateUsers < ActiveRecord::Migration Anpassung des Schemas def up create_table :users do t t.string :first_name t.string :last_name t.timestamps Anpassung rückgängig machen def down drop_table :users 6

Model: User app/models/user.rb DRY class User < ActiveRecord::Base u = User.new first_name: "Obi-Wan", last_name: "Kenobi" u.persisted? # => false u.save u.persisted? # => true puts "#{u.first_name}, #{u.last_name}" # => Obi-Wan Kenobi obi_wan = User.find_by_first_name("Obi-Wan") puts obi_wan.last_name # => Kenobi

Query-API 8

Where-Clauses User.create name: "Nils", age: 24 User.create name: "Luke", age: 42 User.where(name: "Nils") User.where("name =?", "Nils") User.where("name = :name", name: "Nils") User.where(name: "Nils", age: 24) User.where(name: "Nils").where(age: 24) User.where("name =? AND age =?", "Nils", 24) User.where("name =? OR age =?", "Nils", 42) User.where(name: ["Nils", "Luke"]). order("name ASC") User.where("name LIKE 'L%'") 9

SQL-Injections Obacht! User.where("name = #{params[:name]}") Robert ); DROP TABLE Students; -- 10

Associations 11

1:1-Beziehung: Definition Fremdschlüssel: user_id class Avatar < ActiveRecord::Base belongs_to :user class User < ActiveRecord::Base has_one :avatar 12

1:1-Beziehung: Anwung u = User.create name: 'Nils' u.avatar # => nil a = Avatar.new url: 'foo.jpeg' a.persisted? # => false u.avatar = a a.persisted? # => true u.avatar = Avatar.new url: 'bar.jpeg' Avatar.all # => [ # #<Avatar id: 1, url: "foo.jpeg", user_id: nil>, # #<Avatar id: 2, url: "bar.jpeg", user_id: 1> # ] 13

Abhängigkeiten class Avatar < ActiveRecord::Base belongs_to :user class User < ActiveRecord::Base has_one :avatar, depent: :destroy 14

1:n-Beziehung: Definition Fremdschlüssel: user_id class Article < ActiveRecord::Base belongs_to :user class User < ActiveRecord::Base has_many :articles 15

1:n-Beziehung: Anwung u = User.first u.articles # => nil u.articles.new title: "Title" # => #<Article id: nil, title: "Title", user_id: 1> u.save u.articles.first # => => #<Article id: 1, title: "Title", user_id: 1> u.articles << Article.new( title: "Another Title" ) u.articles.count # => 2 u.articles.delete Article.find(1) u.articles.count # => 1 u.articles.clear u.articles.count # => 0 Article.count # => 2 16

n:m-beziehung: Definition class Article < ActiveRecord::Base has_and_belongs_to_many :tags class Tag < ActiveRecord::Base has_and_belongs_to_many :articles class CreateArticlesTags < ActiveRecord::Migration def change create_table :articles_tags, id: false do t t.references :article, null: false t.references :tag, null: false 17 Sortiert

Validations ActiveRecord Grundlagen 18

Validations: Grundlagen class User < ActiveRecord::Base validates_numericality_of :age validates_presence_of :name u = User.new age: "foo" u.valid? # => false u.errors[:age] # => "is not a number" u.errors[:name] # => "can't be blank" 19

Validations: Parameter validates_presence_of :name, message: "Y u no use name?" validates_numericality_of :age, on: :create validates_length_of :name, within: 6..42 validates :name, presence: true, length: { minimum: 6 }, uniqueness: true 20

Validierung mit Methode class Feed < ActiveRecord::Base validate :valid_feed_url protected def valid_feed_url result = Feedzirra::Feed.fetch_and_parse(self.url) if result.nil? result.kind_of?(fixnum) errors.add(:url, "The feed url was not valid.") 21

Callbacks ActiveRecord Grundlagen 22

Callback-Registrierung class Beethoven < ActiveRecord::Base before_destroy :last_words protected def last_words logger.info "Fris applaud, the comedy is over." class Beethoven < ActiveRecord::Base before_destroy do logger.info "Fris applaud, the comedy is over." before_save(on: create) do logger.info "Here we go." 23

Mögliche Callbacks before_validation, before_validation_on_create after_validation, after_validation_on_create before_save before_create, after_create before_destroy, after_destroy 24

Beispiel: Geocoding class Address < ActiveRecord::Base include GeoKit::Geocoders before_save :geolocate validates_presence_of :street, :city, :state, :zip def to_s "#{street} #{city} #{state} #{zip}" protected def geolocate res = GoogleGeocoder.geocode(to_s) self.lattitude = res.lat self.longitude = res.lng 25

Verarbeitung abbrechen def geolocate res = GoogleGeocoder.geocode(to_s) if res.success self.lattitude = res.lat self.longitude = res.lng else errors[:base] = "Geocoding failed. Please check address." false Abbruch der Verarbeitung, wenn ein Callback false liefert 26

Views mit HTML & ERB 27

HTML-Gerüst <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> <title>titel der Webseite</title> <!-- Kommentare werden nicht angezeigt. --> </head> <body> <div id="header" class="container"> <h1>überschrift</h1> <p>inhalt <em>der</em> Webseite</p> </div> </body> </html> 28

Embedded Ruby (ERB) <div id='user-<%= @user.id %>'> <h2> <%= @user.first_name %> <%= @user.last_name %> </h2> <p>age: <%= @user.age %></p> </div> Einbetten des Rückgabewertes eines Ruby-Ausdruckes 29

ERB und Schleifen <div id="squares"> <% 1.upto(3) do i %> <p> The square of <%= i %> is <%= i*i %>.</p> <% %> </div> Ausführung ohne Einbettung <div id="squares"> <p> The square of 1 is 1.</p> <p> The square of 2 is 4.</p> <p> The square of 3 is 9.</p> </div> 30

Controller & Routing 31

Controller: UsersController app/controllers/users_controller.rb class UsersController < ApplicationController def show @user = User.find(params[:id]) app/config/routes.rb MyRailsApp::Application.routes.draw do get 'users/:id' => "users#show"

RESTful Controller Actions HTTP Verb URI Action GET /users index POST /users create GET /users/:id show PUT /users/:id update DELETE /users/:id destroy GET /users/new new GET /users/:id/edit edit 33

Demo: Blog 34

Anforderungen: Blog Category Ein Blog besteht aus Artikeln Artikel gehören zu einer Kategorie Ein Artikel kann mit mehreren Schlagworten versehen sein 1 n Article n m Tag 35