在rails中构建where和or查询4

时间:2018-10-04 02:53:21

标签: ruby-on-rails activerecord

我正在尝试建立一个查询,该查询将显示具有特色的帖子,或者该帖子属于自动批准的用户。

我尝试了几次运气不好的尝试。有什么想法吗?

@posts = Post.includes(users:).where('posts.featured = ? OR users.auto_approved = ?', true, true).order("posts.created_at DESC")

2 个答案:

答案 0 :(得分:3)

尝试一下:

Post.joins(:user).where('posts.featured OR users.auto_approved').order("posts.created_at DESC")

答案 1 :(得分:2)

在这种情况下,您应该使用left_join,因为您希望保留不属于任何users的帖子

@posts = Post.joins('LEFT JOIN users ON users.id = posts.user_id').where('posts.featured = ? OR users.auto_approved = ?', true, true).order("posts.created_at DESC")
相关问题