Home>

I have a question about the method of the follow function of the rails tutorial.
Why should follow (other_user) following< On the contrary, why should unfollow (other_user) not be following.find_by (followed_id: other_user.id) .destroy?
Do you not understand it properly, or is it not good either? I will think.

Code I want to ask user.rb
#Follow users
  def follow (other_user)
    following<<other_user
  end
  #Unfollow user
  def unfollow (other_user)
    active_relationships.find_by (followed_id: other_user.id) .destroy
  end
  # Returns true if the current user is following
  def following? (other_user)
    following.include? (other_user)
  end


user.rb
has_many: active_relationships, class_name: "Relationship",
                                  foreign_key: "follower_id",
                                  dependent:: destroy
  has_many: following, through:: active_relationships, source:: followed
  • Answer # 1

    Why should follow (other_user) following<

    This does not hold.active_relationshipsThe element ofRelationshipAndUserIs not.

    Why should unfollow (other_user) not be following.find_by (followed_id: other_user.id) .destroy?

    If you want to delete the entire user, the latter is fine, but that's not what you want to do, right?