Rails - Letkion III

Transcription

Rails - Letkion III
Ruby on Rails
Lektion III
Inhalt
●
Migrationen
●
Rails-Console / ActiveRecord Query-Interface
●
Erstellung eines Datensatzes
–
●
Suchen eines Datensatzes
–
●
new, save
where, all, find
Validierung
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
2/10
Migrationen
●
Datenbankmanipulation
●
Domain Specific Language (DSL)
●
=> Unabhängig vom DBMS
●
Migrationen enthalten
–
Hilfsmethoden zur Manipulation von
●
●
●
–
●
Tabellen
Spalten
Indizes
Zusicherungen
Verzeichnis: db/migrate
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
3/10
Migrationen
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :login
t.string :name
t.string :email
t.string :password
t.timestamps
end
add_index :users, [:login, :email], unique: true
end
end
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
4/10
Migrationen
●
●
Migrationen ausführen
$ rake db:migrate
Ausgabe:
$ rake db:migrate
== 20140828092230 CreateUsers: migrating===============
-- create_table(:users)
-> 0.0026s
-- add_index(:users, [:login, :email], {:unique=>true})
-> 0.0005s
== 20140828092230 CreateUsers: migrated (0.0032s)======
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
5/10
Rails-Console
●
●
●
IRB mit Rails Environment
Starten:
$ rails console
Datenbankzugriff testen (ohne Validierung)
2.1.2 :001 > User.count
=> 0
2.1.2 :002 > u = User.new
=> #<User id: nil, login: nil, name: nil, email: nil,
password: nil, created_at: nil, updated_at: nil>
2.1.2 :003 > u.name = "fate"
=> "fate"
2.1.2 :004 > u.email = "[email protected]"
=> "[email protected]"
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
6/10
Rails-Console
2.1.2 :005 > u.save
(0.2ms) begin transaction
SQL (4.3ms) INSERT INTO "users" ("created_at",
"email", "name", "updated_at") VALUES (?, ?, ?, ?)
[["created_at", Fri, 09 Aug 2013 17:16:43 UTC +00:00],
["email", "[email protected]"], ["name", "fate"],
["updated_at", Fri, 09 Aug 2013 17:16:43 UTC +00:00]]
(53.0ms) commit transaction
=> true
2.1.2 :006 > User.count
(0.2ms) SELECT COUNT(*) FROM "users"
=> 1
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
7/10
ActiveRecord: Query-Interface
●
where / all / find
2.1.2 :001 > User.where name: 'fate'
=> #<ActiveRecord::Relation [#<User id: 1, login: nil,
name: "fate", email: "[email protected]", password:
nil, created_at: "2014-08-28 09:33:22", updated_at:
"2014-08-28 09:33:22">]>
2.1.2 :002 > User.all.size
=> 1
2.1.2 :003 > User.find(1)
=> #<User id: 1, login: nil, name: "fate", email:
"[email protected]", password: nil, created_at:
"2014-08-28 09:33:22", updated_at: "2014-08-28
09:33:22">
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
8/10
Validierung
class User < ActiveRecord::Base
validates :email, presence: true
validates :password, length: { in: 6..20 }
end
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
9/10
Validierung testen
2.1.2 :001 > u=User.new
=> #<User id: nil, login: nil, name: nil, email: nil,
password: nil, created_at: nil, updated_at: nil>
2.1.2 :002 > u.save
(0.2ms) begin transaction
(0.2ms) rollback transaction
=> false
2.1.2 :003 > u.errors
=> #<ActiveModel::Errors:0x000000019fdc50 @base=#<User
id: nil, login: nil, name: nil, email: nil, password:
nil, created_at: nil, updated_at: nil>,
@messages={:email=>["can't be blank"], :password=>["is
too short (minimum is 6 characters)"]}>
31.08.14 12:51
CS2320 - Ruby on Rails SS2014 - RoR III
10/10