0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

記事に「いいね」機能①

Posted at

Likeモデル作成

データベースのリレーション n対n

多対多ということです!

中間テーブルを作成する!
今回は、likesテーブル

①ブランチの作成「like」

②モデルの作成
ターミナルにて、

$rails g model Like

  create    db/migrate/20230319113154_create_likes.rb
  create    app/models/like.rb

マイグレーションファイル、モデルのファイル作成

③db/migrate/20230319113154_create_likes.rb

  t.references :user, null: false
  t.references :article, null: false

④ターミナルにて

$rails db:migrate

データベースの設定完了

⑤app/models/user.rb

  has_many :likes, dependent: :destroy

⑥app/models/article.rb

  has_many :likes, dependent: :destroy

⑦app/models/like.rb

  belongs_to :user
  belongs_to :article
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?