質問編集履歴

1

modelのコードの追加

2019/04/23 13:40

投稿

TOMSOYA
TOMSOYA

スコア23

test CHANGED
File without changes
test CHANGED
@@ -214,6 +214,90 @@
214
214
 
215
215
  ```
216
216
 
217
+ ```ここに言語を入力
218
+
219
+ #user.rb
220
+
221
+ class User < ApplicationRecord
222
+
223
+ has_secure_password
224
+
225
+ mount_uploader :image, ImageUploader
226
+
227
+
228
+
229
+ validates :name,{presence: true}
230
+
231
+ validates :email,{presence: true, uniqueness: true}
232
+
233
+
234
+
235
+ validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
236
+
237
+ validates :profile, length: { maximum: 160 }
238
+
239
+
240
+
241
+ has_many :posts, dependent: :destroy
242
+
243
+ has_many :comments, dependent: :destroy
244
+
245
+
246
+
247
+ def posts
248
+
249
+ return Post.where(user_id: self.id)
250
+
251
+ end
252
+
253
+
254
+
255
+
256
+
257
+ end
258
+
259
+ ```
260
+
261
+ ```ここに言語を入力
262
+
263
+ #post.rb
264
+
265
+ class Post < ApplicationRecord
266
+
267
+ mount_uploader :image, ImageUploader
268
+
269
+
270
+
271
+ validates :content,{presence: true, length:{maximum:140}}
272
+
273
+ validates :user_id,{presence: true}
274
+
275
+
276
+
277
+ belongs_to :user
278
+
279
+ has_many :comments, dependent: :destroy
280
+
281
+
282
+
283
+
284
+
285
+ def user
286
+
287
+ return User.find_by(id: self.user_id)
288
+
289
+ end
290
+
291
+
292
+
293
+
294
+
295
+ end
296
+
297
+
298
+
299
+ ```
300
+
217
301
 
218
302
 
219
303
  ### 試したこと