Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如果某个用户没有跟帖,请停止跟帖_Ruby_Methods_If Statement_Ruby 1.9 - Fatal编程技术网

Ruby 如果某个用户没有跟帖,请停止跟帖

Ruby 如果某个用户没有跟帖,请停止跟帖,ruby,methods,if-statement,ruby-1.9,Ruby,Methods,If Statement,Ruby 1.9,当且仅当我已经离开去关注一个用户的所有帖子时,我才想停止关注他。 为此,我尝试了这种方法,但不适用于我 user_with_posts = User.find(params[:id]) posts_of_users = user_with_posts.posts posts_of_users.each do |post| current_user.unfollow(user_with_posts) unless current_user.follows?(post) end 这里的问题是,如果

当且仅当我已经离开去关注一个用户的所有帖子时,我才想停止关注他。

为此,我尝试了这种方法,但不适用于我

user_with_posts = User.find(params[:id])
posts_of_users = user_with_posts.posts
posts_of_users.each do |post|
current_user.unfollow(user_with_posts) unless current_user.follows?(post)
end
这里的问题是,如果有帖子的用户有3篇帖子,而当前用户只关注1篇帖子,那么代码将运行当前用户。unfollow(有帖子的用户)

我只想运行**当前用户。取消跟踪(带帖子的用户)如果且仅当我已经离开并跟随带帖子的用户的所有帖子时

您可以用于此目的:

有吗?[{obj | block}]→ 真假

将集合的每个元素传递给给定的块。方法 如果块返回的值不是false或false,则返回true 无如果没有给出该块,Ruby将添加一个{| obj的隐式块| 如果集合中至少有一个 成员不为假或零

user_with_posts = User.find(params[:id])
posts_of_users = user_with_posts.posts
current_user.unfollow(user_with_posts) unless posts_of_users.any? do |post|
  current_user.follows?(post)
end