如何在模型中创建仅返回关联等级大于 current_user 等级的那些对象的范围? [英] How can I create a scope in a model that returns only those objects with an associated rank greater than the current_user's rank?

查看:49
本文介绍了如何在模型中创建仅返回关联等级大于 current_user 等级的那些对象的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Page 模型中创建一个 :readable 范围,以返回 current_personPages代码> 有足够的等级"可供阅读:

I am trying to create a :readable scope in my Page model to return all the Pages that the current_person has sufficient 'rank' to read:

scope :readable, lambda { |current_person| joins(:role_readable)
    .where(:role_readable[:rank].gte(current_person.roles.first.rank) ) }

我尝试了许多范围排列,包括这个,但都没有成功(上面的那个给出了无法将符号转换为整数"的错误).

I've tried many scope permutations, including this one, with no success (the one above gives a "can't convert Symbol into Integer" error).

问题变得更加复杂,因为Users(处理身份验证等/与帐户同义)有_many People,代表User存在于一个组织中——即.一个 User 可以是多个组织的成员,但只需要一种登录方式...

The problem is made more complex because Users (which handle authentication etc. / synonymous with accounts) have_many People, which represent the User's presence in an organization — ie. a User could be a member of multiple organizations, but only needs one way of logging in...

User's People 拥有的 Roles 有_many,给出范围用来返回 的等级:可读页面.(即,User 处理身份验证,而 People 处理授权(除其他事项外),因为 User 可以是多个组织的成员,其中用户具有不同的角色,因此具有不同的层次结构和权限.)

The Roles that a User's People has_many of, give the rank that the scope uses to return the :readable pages. (ie. Users handle authentication, whereas People handle authorization (among other things), as a User could be a member of multiple organizations for which the user has different Roles, and thus heirarchy and privilege.)

class User < ActiveRecord::Base  
  has_many :people
  has_many :roles, through: :people    

class Person < ActiveRecord::Base
  belongs_to :user
  has_and_belongs_to_many :roles

class Role < ActiveRecord::Base
  #  name       :string(255)
  #  rank       :integer 
  has_and_belongs_to_many :people

class Page < ActiveRecord::Base
  belongs_to :role_readable, class_name: "Role", foreign_key: :role_read_id
  belongs_to :role_editable, class_name: "Role", foreign_key: :role_write_id

角色具有记录为rank(整数)的层次结构(例如,Boss、Manager、Worker...)——整数越小,层次结构(以及特权)越大.如果任何一个人的 Roles 的等级小于或等于 :role_readablePage,则用户有权查看页面.

Roles have a hierarchy (eg. Boss, Manager, Worker...) recorded as a rank (integer) — the lower the integer the greater the hierarchy (and thus privilege). If any one of the Person's Roles has a rank less than or equal to the :role_readable of a Page then the user has privilege to view the page.

例如.具有 Manager 角色的 Person 可以查看具有 :role_readableManagerWorker<的所有页面/code>,但不是 Boss.我需要一个列出所有此类可读页面的范围.

Eg. a Person with a Manager role could view all pages with a :role_readable of Manager or Worker, but not Boss. I need a scope that lists all such readable pages.

Roles 有一个 default_scope 来确保它们按排名的升序返回,因此 Personrole.first 将返回角色具有最高等级(即最低等级值).

Roles have a default_scope that ensures they are returned in ascending order of rank, so role.first for a Person will return the role with the greatest hierarchy (ie. lowest rank value).

我使用的是 Rails 3.2 和 Postgres 9.2.2.0,所以我也对许多可用的查询/Arel 选项感到困惑.

I'm using Rails 3.2 and Postgres 9.2.2.0, so am also confusing myself with the many query / Arel options available.

提前致谢!

更新:

如果我尝试这个范围:

scope :readable, lambda { |current_person| where("role_readable.rank
  => ?",current_person.roles.first.rank) }

我收到此错误:

PGError: ERROR:  missing FROM-clause entry for table "role_readable"
LINE 1: ...*) FROM "pages"  WHERE "pages"."team_id" = 2 AND (role_reada...
                                                         ^
: SELECT COUNT(*) FROM "pages"  WHERE "pages"."team_id" = 2 AND (role_readable.rank => 1)

我不知道如何理解缺少的 FROM 子句"

I'm not sure what to make of the 'missing FROM-clause'

推荐答案

经过多次尝试,答案很简单:

And the answer is quite simple, after much experimenting:

scope :readable, lambda { |current_person| joins(:role_readable)
      .where(["roles.rank >= ?", current_person.roles.first.rank]) }

尽管 join 指的是关联名称 :role_readablewhere 必须使用表名——即.roles.rank 不是 role_readable.rank.

Although the join refers to the association name :role_readable, the where must use the table name — ie. roles.rank not role_readable.rank.

新玩家的陷阱?显然.

感谢 ctcherry 和他的回答 这里 放弃了游戏.

Kudos to ctcherry and his answer here which gave the game away.

这篇关于如何在模型中创建仅返回关联等级大于 current_user 等级的那些对象的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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