質問編集履歴

1

rails cの結果とusermodelのコードを追加

2019/04/30 03:24

投稿

TOMSOYA
TOMSOYA

スコア23

test CHANGED
File without changes
test CHANGED
@@ -158,6 +158,98 @@
158
158
 
159
159
  ```
160
160
 
161
+ ```ここに言語を入力
162
+
163
+ user = User.create(name: "taro", email: "taro@example.com", password: "foo", password_confirmation: "foo")
164
+
165
+ (0.1ms) begin transaction
166
+
167
+ User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "taro@example.com"], ["LIMIT", 1]]
168
+
169
+ User Create (1.6ms) INSERT INTO "users" ("name", "email", "created_at", "updated_at", "password_digest") VALUES (?, ?, ?, ?, ?) [["name", "taro"], ["email", "taro@example.com"], ["created_at", "2019-04-28 21:47:51.443175"], ["updated_at", "2019-04-28 21:47:51.443175"], ["password_digest", "$2a$10$A6kLyqa5sEYJhYGKNL1aUOX3kaYAReqq4HYzERSEuO55.TPn7r15S"]]
170
+
171
+ (7.8ms) commit transaction
172
+
173
+ => #<User:0x005608d9b11718
174
+
175
+ id: 1,
176
+
177
+ name: "taro",
178
+
179
+ email: "taro@example.com",
180
+
181
+ created_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00,
182
+
183
+ updated_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00,
184
+
185
+ image_name: nil,
186
+
187
+ password_digest:
188
+
189
+ "$2a$10$A6kLyqa5sEYJhYGKNL1aUOX3kaYAReqq4HYzERSEuO55.TPn7r15S",
190
+
191
+ profile: nil>
192
+
193
+ [2] pry(main)> user.authenticate("foo")
194
+
195
+ => #<User:0x005608d9b11718
196
+
197
+ id: 1,
198
+
199
+ name: "taro",
200
+
201
+ email: "taro@example.com",
202
+
203
+ created_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00,
204
+
205
+ updated_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00,
206
+
207
+ image_name: nil,
208
+
209
+ password_digest:
210
+
211
+ "$2a$10$A6kLyqa5sEYJhYGKNL1aUOX3kaYAReqq4HYzERSEuO55.TPn7r15S",
212
+
213
+ profile: nil>
214
+
215
+ ```
216
+
217
+ ```ここに言語を入力
218
+
219
+ class User < ApplicationRecord
220
+
221
+ has_secure_password validations: true
222
+
223
+ mount_uploader :image, ImageUploader
224
+
225
+
226
+
227
+ validates :name,{presence: true}
228
+
229
+ validates :email,{presence: true, uniqueness: true}
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+ has_many :posts, dependent: :destroy
238
+
239
+
240
+
241
+ def posts
242
+
243
+ return Post.where(user_id: self.id)
244
+
245
+ end
246
+
247
+ end
248
+
249
+
250
+
251
+ ```
252
+
161
253
 
162
254
 
163
255
  ### 試したこと