ruby-on-rails - 如何选择除当前用户之外的所有用户
问题描述
我想在索引页面上列出除当前用户之外的所有用户。我试图制作一个“关注的人”功能,显然当前用户不能在该列表中。我当前的代码如下。我正在使用设备,我假设类似@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 %>
解决方案
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)
推荐阅读
- python - 遍历数组时出现ValueError
- html - 为 PostCSS 设置配置文件
- google-apps-script - Apps 脚本:如何以编程方式调整带有包裹单元格的 Google 表格行的大小,使其小于包裹的文本
- linux - How to Get Fully Functional PowerShell running on Linux?
- amazon-web-services - Golang AWS API Gateway 无效字符“e”寻找值的开头
- javascript - bind 如何与 new 和 instanceof 一起工作?
- sql - Query result in a single row or multiple values in single with comma separated
- javascript - Firebase how to find child knowing its id but not its parent's id (js)
- spring - Spring Boot:错误:提交响应后无法调用 sendError()
- html - 如何在组件的返回值中设置跨度标签