首页 > 解决方案 > 如何选择除当前用户之外的所有用户

问题描述

我想在索引页面上列出除当前用户之外的所有用户。我试图制作一个“关注的人”功能,显然当前用户不能在该列表中。我当前的代码如下。我正在使用设备,我假设类似@users = User.where(user: != current_user).limit(5) 或类似的东西。

推文控制器,索引:

@users = User.all.limit(5)

索引页面:

<h3 class="text-primary sides-title">Explore</h3>
<% @users.each do |user| %>
  <div class="user_card">
     <p class="users_username text-primary"><%= user.username %></p>
     <%=  cl_image_tag user.photo, height: 60, width: 60, crop: :fill, class: "users_photo" %>
     <%= link_to "Check'em out!", user_path(user), class: "user_button" %>
  </div>
<% end %>

标签: ruby-on-railsruby

解决方案


User.where(user: != current_user)是无效的语法,对于 SQL 比较运算符,您可以提供一个字符串作为参数where

User.where('user_id != ?', current_user.id) # binding the argument provided

要不就User.where.not(id: current_user.id)


推荐阅读