Cap 6
6.1.1
Ex1)
The schema file contains infos of the migration file, and also exists fields created_at and update_at.
Ex2)
The schema file became empty after the command db:rollback
Ex3)
ActiveRecord::Schema.define(version: 20170923154412) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
6.1.2
Ex1)
irb(main):009:0> x = User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
irb(main):010:0> x.class
=> User(id: integer, name: string, email: string, created_at: datetime, updated_at: datetime)
Ex2)
irb(main):003:0> c.class.superclass
=> ApplicationController
irb(main):004:0> c.class.superclass.superclass
=> ActionController::Base
6.1.3
Ex1)
irb(main):011:0> user.name.class
=> String
irb(main):012:0> user.email.class
=> String
Ex2)
irb(main):013:0> user.created_at.class
=> ActiveSupport::TimeWithZone
irb(main):014:0> user.updated_at.class
=> ActiveSupport::TimeWithZone
6.1.4
Ex1)
irb(main):017:0> User.find_by(name: "Norton")
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."name" = ? LIMIT ? ["name", "Norton"], ["LIMIT", 1](/nortonbino/sample_app/wiki/"name",-"Norton"],-["LIMIT",-1)
=> #<User id: 1, name: "Norton", email: "[email protected]", created_at: "2017-09-23 16:05:48", updated_at: "2017-09-23 16:05:48">
Ex2)
irb(main):018:0> User.all.class
=> User::ActiveRecord_Relation
Ex3)
irb(main):020:0> User.all.length
User Load (0.4ms) SELECT "users".* FROM "users"
=> 2
6.1.5
Ex1)
irb(main):030:0> user.name = "Norton"
=> "Norton"
irb(main):031:0> user.save
(0.2ms) SAVEPOINT active_record_1
SQL (0.4ms) UPDATE "users" SET "name" = ?, "updated_at" = ? WHERE "users"."id" = ? ["name", "Norton"], ["updated_at", "2017-09-23 16:20:18.196263"], ["id", 1](/nortonbino/sample_app/wiki/"name",-"Norton"],-["updated_at",-"2017-09-23-16:20:18.196263"],-["id",-1)
(0.2ms) RELEASE SAVEPOINT active_record_1
=> true
Ex2)
irb(main):032:0> user.update_attribute(:email, "[email protected]")
(0.2ms) SAVEPOINT active_record_1
SQL (0.6ms) UPDATE "users" SET "email" = ?, "updated_at" = ? WHERE "users"."id" = ? ["email", "[email protected]"], ["updated_at", "2017-09-23 16:21:34.834345"], ["id", 1](/nortonbino/sample_app/wiki/"email",-"[email protected]"],-["updated_at",-"2017-09-23-16:21:34.834345"],-["id",-1)
(0.2ms) RELEASE SAVEPOINT active_record_1
=> true
Ex3)
irb(main):034:0> user.created_at = 1.year.ago
=> Fri, 23 Sep 2016 16:23:14 UTC +00:00
irb(main):035:0> user.save
(0.2ms) SAVEPOINT active_record_1
SQL (0.3ms) UPDATE "users" SET "created_at" = ?, "updated_at" = ? WHERE "users"."id" = ? ["created_at", "2016-09-23 16:23:14.868673"], ["updated_at", "2017-09-23 16:23:25.341497"], ["id", 1](/nortonbino/sample_app/wiki/"created_at",-"2016-09-23-16:23:14.868673"],-["updated_at",-"2017-09-23-16:23:25.341497"],-["id",-1)
(0.2ms) RELEASE SAVEPOINT active_record_1
=> true
6.2.1
Ex3)
irb(main):001:0> n = User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
irb(main):002:0> n.name = "Issao"
=> "Issao"
irb(main):003:0> n.email = "[email protected]"
=> "[email protected]"
irb(main):004:0> n.save
(0.2ms) begin transaction
SQL (0.3ms) INSERT INTO "users" ("name", "email", "created_at", "updated_at") VALUES (?, ?, ?, ?) ["name", "Issao"], ["email", "[email protected]"], ["created_at", "2017-09-23 16:26:39.365744"], ["updated_at", "2017-09-23 16:26:39.365744"](/nortonbino/sample_app/wiki/"name",-"Issao"],-["email",-"[email protected]"],-["created_at",-"2017-09-23-16:26:39.365744"],-["updated_at",-"2017-09-23-16:26:39.365744")
(74.7ms) commit transaction
=> true
irb(main):005:0> n.valid?
=> true
Ex2)
irb(main):009:0> user = User.find(1)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? ["id", 1], ["LIMIT", 1](/nortonbino/sample_app/wiki/"id",-1],-["LIMIT",-1)
=> #<User id: 1, name: "Issao", email: "[email protected]", created_at: "2017-09-23 16:26:39", updated_at: "2017-09-23 16:26:39">
irb(main):010:0> user.valid?
=> true
6.2.2
Ex1)
irb(main):001:0> u = User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
irb(main):002:0> u.valid?
=> false
irb(main):003:0> u.errors.full_messages
=> ["Name can't be blank", "Email can't be blank"]
Ex2)
irb(main):005:0> u.errors.messages
=> {:name=>["can't be blank"], :email=>["can't be blank"]}
6.2.3
Ex1)
irb(main):001:0> u = User.new(name: "a" * 51, email: "a" * 244 + "@example.com")
=> #<User id: nil, name: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...", email: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...", created_at: nil, updated_at: nil>
irb(main):002:0> u.valid?
=> false
Ex2)
irb(main):003:0> u.errors.messages
=> {:name=>["is too long (maximum is 50 characters)"], :email=>["is too long (maximum is 255 characters)"]}
6.3.2
Ex1)
irb(main):001:0> u = User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil>
irb(main):002:0> u.name = "norton"
=> "norton"
irb(main):003:0> u.email = "[email protected]"
=> "[email protected]"
irb(main):004:0> u.valid?
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ? ["email", "[email protected]"], ["LIMIT", 1](/nortonbino/sample_app/wiki/"email",-"[email protected]"],-["LIMIT",-1)
=> false
Ex2)
irb(main):005:0> u.errors.messages
=> {:password=>["can't be blank"]}
6.3.3
Ex1)
irb(main):001:0> u = User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil>
irb(main):002:0> u.name = "Norton"
=> "Norton"
irb(main):003:0> u.email = "[email protected]"
=> "[email protected]"
irb(main):005:0> u.password = "aaaa"
=> "aaaa"
irb(main):006:0> u.password_confirmation = "aaaa"
=> "aaaa"
irb(main):007:0> u.valid?
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ? ["email", "[email protected]"], ["LIMIT", 1](/nortonbino/sample_app/wiki/"email",-"[email protected]"],-["LIMIT",-1)
=> false
Ex2)
irb(main):008:0> u.errors.messages
=> {:password=>["is too short (minimum is 6 characters)"]}