質問編集履歴

1

誤字

2020/09/09 16:00

投稿

Asahi_sun_
Asahi_sun_

スコア5

test CHANGED
File without changes
test CHANGED
@@ -259,3 +259,179 @@
259
259
  <% end %>
260
260
 
261
261
  ```
262
+
263
+
264
+
265
+ commentモデル
266
+
267
+ ```ここに言語を入力
268
+
269
+ class Comment < ApplicationRecord
270
+
271
+ belongs_to :user
272
+
273
+ belongs_to :book
274
+
275
+ validates :content, presence: true, length: { maximum: 30 }
276
+
277
+ end
278
+
279
+ ```
280
+
281
+ bookモデル
282
+
283
+ ```ここに言語を入力
284
+
285
+ class Book < ApplicationRecord
286
+
287
+ belongs_to :user
288
+
289
+ has_many :likes, dependent: :destroy
290
+
291
+ has_many :liked_user, through: :likes, source: :user
292
+
293
+ has_many :comments, dependent: :destroy
294
+
295
+ has_many :commented_user, through: :comments, source: :user
296
+
297
+
298
+
299
+ default_scope -> { order(created_at: :desc) }
300
+
301
+ validates :title, presence: true, length: { maximum: 100 }
302
+
303
+ validates :author, presence: true, length: { maximum: 50 }
304
+
305
+ validates :publisher, presence: true, length: { maximum: 50 }
306
+
307
+ validates :publish_year, presence: true
308
+
309
+ validates :user_id, presence: true
310
+
311
+ validates :explanation, length: { maximum: 140 }
312
+
313
+ end
314
+
315
+ ```
316
+
317
+
318
+
319
+ userモデル
320
+
321
+ ```ここに言語を入力
322
+
323
+ class User < ApplicationRecord
324
+
325
+ has_many :books, dependent: :destroy
326
+
327
+ has_many :active_relationships, class_name: "Relationship",
328
+
329
+ foreign_key: "follower_id",
330
+
331
+ dependent: :destroy
332
+
333
+ has_many :passive_relationships, class_name: "Relationship",
334
+
335
+ foreign_key: "followed_id",
336
+
337
+ dependent: :destroy
338
+
339
+ has_many :following, through: :active_relationships, source: :followed
340
+
341
+ has_many :followers, through: :passive_relationships, source: :follower
342
+
343
+ has_many :likes, dependent: :destroy
344
+
345
+ has_many :liked_books, through: :likes, source: :book
346
+
347
+ has_many :comments, dependent: :destroy
348
+
349
+ has_many :commented_books, through: :comments, source: :book
350
+
351
+ before_save { self.email.downcase! }
352
+
353
+ validates :name, presence: true, length: { maximum: 50 }
354
+
355
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
356
+
357
+ validates :email, presence: true, length: { maximum: 255 },
358
+
359
+ format: { with: VALID_EMAIL_REGEX },
360
+
361
+ uniqueness: true
362
+
363
+ has_secure_password
364
+
365
+ validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
366
+
367
+
368
+
369
+ #渡された文字列のハッシュ値を返す
370
+
371
+ def User.digest(string)
372
+
373
+ cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
374
+
375
+ BCrypt::Engine.cost
376
+
377
+ BCrypt::Password.create(string, cost: cost)
378
+
379
+ end
380
+
381
+
382
+
383
+ def feed
384
+
385
+ following_ids = "SELECT followed_id FROM relationships
386
+
387
+ WHERE follower_id = :user_id"
388
+
389
+ Book.where("user_id IN (#{following_ids})
390
+
391
+ OR user_id = :user_id", user_id: id)
392
+
393
+ end
394
+
395
+
396
+
397
+ def follow(other_user)
398
+
399
+ following << other_user
400
+
401
+ end
402
+
403
+
404
+
405
+ def unfollow(other_user)
406
+
407
+ active_relationships.find_by(followed_id: other_user.id).destroy
408
+
409
+ end
410
+
411
+
412
+
413
+ def following?(other_user)
414
+
415
+ following.include?(other_user)
416
+
417
+ end
418
+
419
+
420
+
421
+ def followed_by?(other_user)
422
+
423
+ followers.include?(other_user)
424
+
425
+ end
426
+
427
+
428
+
429
+ def already_liked?(book)
430
+
431
+ self.likes.exists?(book_id: book.id)
432
+
433
+ end
434
+
435
+ end
436
+
437
+ ```