简体   繁体   English

在“显示操作”中显示当前帖子以外的其他帖子

[英]Show posts other than current post on Show Action

This is probably an easy question, but can't find it for the life of me.. 这可能是一个简单的问题,但在我的一生中找不到。

On a post's show action, I'm trying to show in a small sidebar all of the other posts of that user besides the current post. 在帖子的显示操作中,我试图在小边栏中显示该用户除当前帖子外的所有其他帖子。 Im using a loop with 我正在使用一个循环

@post.user.posts.limit(6).shuffle.each do 

i would think you could do 我认为你可以做到

.except(@post)

or something similar to exclude the current post but that didn't do the trick. 或类似的东西排除当前职位,但没有成功。 Does anyone know how to do this?? 有谁知道如何做到这一点??

You can easily do it via SQL: 您可以通过SQL轻松完成此操作:

@post.user.posts.where("posts.id <> ?", @post.id).limit(6).shuffle.each do
# same thing but wrote differently:
@post.user.posts.where("posts.id NOT IN (?)", @post.id).limit(6).shuffle.each do 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails:在其他模型的show动作中创建新帖子 - Rails: Creating new posts in the other model's show action 如何显示当前用户的帖子和所有帖子评论? - How to show the current user's post and the all posts comments? Ruby on Rails:devise:,我如何允许帖子/索引和帖子/显示操作对所有操作,但其他操作将被拒绝 - Ruby on rails: devise :, how can i allow posts/index and posts/show action to all but other actions will be denied 没有路由匹配{:action =&gt;“ show_post_category”,:category_id =&gt; 5,:controller =&gt;“ posts”} - No route matches {:action=>“show_post_category”, :category_id=>5, :controller=>“posts”} 使用onclick调用rails“ posts#show”操作 - Call rails “posts#show” action with onclick 没有路线匹配{:controller =&gt;“ posts”,:action =&gt;“ show”} - No route matches {:controller=>“posts”, :action=>“show”} 没有路线匹配{:action =&gt;“show”...:controller =“posts”} - No route matches {:action=>“show”… :controller = “ posts” } Rails:显示除最后一个帖子以外的所有帖子 - Rails: Show all posts except last post 索引动作重定向以显示没有发布的动作 - Index action redirecting to show action without post show.html.erb页面,供current_user以外的用户查看 - show.html.erb page for viewing by users other than current_user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM