yuyanegishi / issue_check

0 stars 0 forks source link

★Rails Turtorial:Unfollowボタンを押した時に挙動 #8

Open yuyanegishi opened 5 years ago

yuyanegishi commented 5 years ago
def destroy
        @user = Relationship.find(params[:id]).followed
        current_user.unfollow(@user)
        respond_to do |format|
            format.html { redirect_to @user }
            format.js
        end
end
yuyanegishi commented 5 years ago

確認コード

@user = Relationship.find(params[:id]).followed

分解1

Relationship.find(params[:id])
2018-12-04 16 15 12

分解2

Relationクラスのインスタンス.followed
class Relationship < ApplicationRecord
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"
  validates :follower_id, presence: true
  validates :followed_id, presence: true
end
2018-12-04 16 19 54
yuyanegishi commented 5 years ago

確認コード

current_user.unfollow(@user)
# ユーザーをフォロー解除する
def unfollow(other_user)
  active_relationships.find_by(followed_id: other_user.id).destroy
end

分解1

active_relationships.find_by(followed_id: other_user.id)