Ruby on rails 在rails 4中构建where和or查询

Ruby on rails 在rails 4中构建where和or查询,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我正在尝试构建一个查询,该查询将显示文章(如果有特色)或该文章是否属于自动批准的用户 我尝试了一些变化,但运气不好。有什么想法吗 @posts = Post.includes(users:).where('posts.featured = ? OR users.auto_approved = ?', true, true).order("posts.created_at DESC") 试试这个: Post.joins(:user).where('posts.featured OR users.

我正在尝试构建一个查询,该查询将显示文章(如果有特色)或该文章是否属于自动批准的用户

我尝试了一些变化,但运气不好。有什么想法吗

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

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

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

对于这种情况,您应该使用
left\u join
,因为您希望保留不属于任何
用户的帖子

@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")

对于这种情况,您应该使用
left\u join
,因为您希望保留不属于任何
用户的帖子

@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")