Rails查询关联模型范围 [英] Rails querying an associated models scope

查看:66
本文介绍了Rails查询关联模型范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找合适的解决方案来解决应该是一个简单的任务.本质上,我有一个类别模型,其中有很多 帖子.帖子属于类别.

I am struggling to find a suitable solution for what should be a simple task. Essentially I have a category model which has many posts. The post belongs to the category.

我正在将类别显示为搜索表单以及其他许多地方的一部分,并且不想显示没有相关文章的类别.这似乎毫无意义.

I am displaying categories as part of a search form as well as a number of other places and do not want to display categories that have no associated posts. That seems kind of pointless.

我设法通过在我的类别模型中添加以下方法来解决此问题.

I managed to solve this problem by adding the following method to my category model.

# Check if Category has associated results
def self.with_results
  includes(:posts).where.not(posts: { id: nil })
end

那很好,我可以过滤没有结果的类别.有点混乱的地方是当我尝试使用范围时.

That works fine allowing me to filter categories that have no results. The slightly more confusing bit is when I try to use scopes.

我的帖子"具有多个范围,例如 frontend_visible ,它规定是否应从前端(非管理员)访问该帖子.

My Post has several scopes such as frontend_visible which dictates whether it should be accessible from the frontend (non-admin).

scope :frontend_visible, -> { where(:state => ['approved', 'changes_pending_approval']) }

同样,我还有其他作用域可以拉出仅标记为私人内容的帖子(仅限会员).

Equally I have other scopes to pull posts that are marked as private content only (members only).

我最初的解决方案的问题是,将不会显示包含未批准帖子的类别,同样,非会员也将无法看到标记为私人的帖子,尽管该类别仍会显示.

The problem with my initial solution is that a category that contains posts which are not approved will not be shown, equally non-members will not be able to see posts that are marked as private although the category will still be shown.

理想的解决方案是:

获取所有具有关联帖子的类别,如果关联帖子在前端不可见,则忽略类别.如果current_user无法访问私有内容,并且所有关联的帖子都标记为私有,则忽略类别.

Get all categories that have associated posts, if associated posts are not frontend visible, disregard category. If current_user can not access private content and all associated posts are marked private, disregard category.

我有范围,但是我不确定如何从关联的模型中使用它们.这可能吗?

I have the scopes but I am unsure how to use them from the associated model. Is this possible?

推荐答案

据我了解,您需要选择对用户可见的帖子类别.为此,您需要做两件事:

As i understand, you need to select categories with posts visible to an user. For that you need to do two things:

  1. 在用户角色和帖子的可见范围之间进行映射
  2. 为用户可见的帖子选择类别.就您而言,在两个查询中选择不连接的类别会更容易.

尝试以下代码:

class Category < ApplicationRecord
  has_many :posts

  scope :with_posts_for_user, -> (user) do
    where(id: Post.categories_for_user(user))
  end
end

class Post < ApplicationRecord
  belongs_to :category

  scope :frontend_visible, -> { where(:state => ['approved', 'changes_pending_approval']) }
  scope :member_visible, -> { where(:state => ['approved', 'changes_pending_approval', 'private']) }

  scope :categories_for_user, -> (user) do
    (user.member? ? member_visible : frontend_visible).distinct.pluck(:category_id)
  end
end

这篇关于Rails查询关联模型范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆