[英]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.