回答編集履歴

3

ソースコード追記

2020/03/09 11:55

投稿

no1knows
no1knows

スコア3365

test CHANGED
@@ -42,6 +42,72 @@
42
42
 
43
43
  ```
44
44
 
45
+
46
+
47
+ ```
48
+
49
+ irb(main):001:0> user1 = User.new(name:'',email:'1@gmail.com')
50
+
51
+ (0.8ms) SELECT sqlite_version(*)
52
+
53
+ => #<User id: nil, name: "", email: "1@gmail.com", created_at: nil, updated_at: nil>
54
+
55
+ irb(main):002:0> user1.save
56
+
57
+ (0.1ms) begin transaction
58
+
59
+ User Exists? (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "1@gmail.com"], ["LIMIT", 1]]
60
+
61
+ (0.0ms) rollback transaction
62
+
63
+ => false
64
+
65
+ irb(main):003:0> user2 = User.new(name:'aaaaa',email:'')
66
+
67
+ => #<User id: nil, name: "aaaaa", email: "", created_at: nil, updated_at: nil>
68
+
69
+ irb(main):004:0> user2.save
70
+
71
+ (0.1ms) begin transaction
72
+
73
+ User Exists? (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", ""], ["LIMIT", 1]]
74
+
75
+ (0.1ms) rollback transaction
76
+
77
+ => false
78
+
79
+ irb(main):005:0> user1 = User.new(name:'あああ',email:'1@gmail.com')
80
+
81
+ => #<User id: nil, name: "あああ", email: "1@gmail.com", created_at: nil, updated_at: nil>
82
+
83
+ irb(main):006:0> user1.save
84
+
85
+ (0.1ms) begin transaction
86
+
87
+ User Exists? (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "1@gmail.com"], ["LIMIT", 1]]
88
+
89
+ User Create (1.0ms) INSERT INTO "users" ("name", "email", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "あああ"], ["email", "1@gmail.com"], ["created_a "2020-03-09 11:46:44.041319"], ["updated_at", "2020-03-09 11:46:44.041319"]]
90
+
91
+ (0.9ms) commit transaction
92
+
93
+ => true
94
+
95
+ irb(main):007:0> user1 = User.new(name:'iii',email:'1@gmail.com')
96
+
97
+ => #<User id: nil, name: "iii", email: "1@gmail.com", created_at: nil, updated_at: nil>
98
+
99
+ irb(main):008:0> user1.save
100
+
101
+ (0.1ms) begin transaction
102
+
103
+ User Exists? (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "1@gmail.com"], ["LIMIT", 1]]
104
+
105
+ (0.0ms) rollback transaction
106
+
107
+ => false
108
+
109
+ ```
110
+
45
111
  ![イメージ説明](a0bdb62db8b9ca9d05a51ec88e14195a.png)
46
112
 
47
113
 

2

追記

2020/03/09 11:55

投稿

no1knows
no1knows

スコア3365

test CHANGED
@@ -43,3 +43,7 @@
43
43
  ```
44
44
 
45
45
  ![イメージ説明](a0bdb62db8b9ca9d05a51ec88e14195a.png)
46
+
47
+
48
+
49
+ 期待する動きとなっているので、バリデーション自体は問題ないかと思います。

1

実行結果追記

2020/03/09 11:54

投稿

no1knows
no1knows

スコア3365

test CHANGED
@@ -22,4 +22,24 @@
22
22
 
23
23
 
24
24
 
25
+ ---
26
+
27
+ とりあえず実行してみました。
28
+
29
+ ```ruby
30
+
31
+ rails new test-app
32
+
33
+ rails model name:string email:string
34
+
35
+ rails db:migrate
36
+
25
- 最後に質問のコードなどはcodeタグ利用して↑のように見やすくしてください。
37
+ #作成されたapp/models/user.rbに上記バリデーション追加
38
+
39
+ rails c
40
+
41
+
42
+
43
+ ```
44
+
45
+ ![イメージ説明](a0bdb62db8b9ca9d05a51ec88e14195a.png)