LoginSignup
tn-soccerball
@tn-soccerball

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

いいね機能のメッソド書き方

app/models/users.erbにいいね機能を記述したいのですがうまくいきません。
主に。likeの引数とcreated_by(.....)内にどのような書き方をすれば良いのかがよく分かりません。

中間テーブルには外部キーとして
user_idとmicropost_idがあります。
```
抜粋

has_many :favorites
has_many :favorites, through: :favorites, source: :micropost

#いいね機能↓

def like(other_user)
self.favorites.find_or_create_by(micropost_id: other_user.id)
end

def unlike
favorite = self.favorites.find_by(micropost_id: other_user.id)
favorite.destroy if favorite
end
```
よろしくお願いいたします。

0

No Answers yet.

Your answer might help someone💌