CanCan 和accessible_by 给出错误,无法确定SQL

发布于 2025-01-03 18:28:38 字数 836 浏览 0 评论 0原文

我需要一些建议。

在我的控制器中,我有一个错误: “accessible_by 调用不能与块‘can’定义一起使用。无法确定 SQL...”

组控制器代码:

@groups = @q.result.order('name').accessible_by(current_ability,:read).page(params[:page]).per(20)

在能力.rb 文件中:

can :read, Group do |group|
        group.try(:group_type) == 1 || group.try(:user) == user || user_just_joined(user, group.id)
end

user_just_joined 是一个函数,如果用户在以下位置相关,则返回 true 或 false这个组,或者不是。

是的,我知道我应该以这种方式准备代码,以避免此错误:

can :read, Group, :group_type => 1
can :read, Group, :user => user
can :read, Group, user_just_joined(user, :id)

// 上面的代码应该创建“OR”语句,类似于第一个示例

但是: can :read, Group, user_just_joined(user, :id ) 不起作用,:id 值未定义。

将“OR”语句与函数一起使用(根据传递的参数返回 true 或 false)的最佳解决方案是什么?

I need some advice.

In my controller, I have an error:
"The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined ..."

Group controller code:

@groups = @q.result.order('name').accessible_by(current_ability,:read).page(params[:page]).per(20)

In ability.rb file:

can :read, Group do |group|
        group.try(:group_type) == 1 || group.try(:user) == user || user_just_joined(user, group.id)
end

user_just_joined is a function, that return true or false, if user is related in this group, or not.

Yes, i know that I should prepare code in this way, to avoid this error:

can :read, Group, :group_type => 1
can :read, Group, :user => user
can :read, Group, user_just_joined(user, :id)

// above code shoult create "OR" statement, analogical to first example

But: can :read, Group, user_just_joined(user, :id) doesn't work, :id value is undefined.

What is the best solution to using an "OR" statement with function, that return true or false dependent on passed params?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2025-01-10 18:28:38

从文档来看,这可能会有所帮助...accessible_by 是一个类方法,因此类似于本例中的索引操作。

获取记录

块的条件只能通过 Ruby 执行。如果您使用accessible_by 获取记录,则会引发异常。要从数据库中获取记录,您需要提供表示条件的 SQL 字符串。 SQL 将进入 WHERE 子句,如果需要进行联接,请考虑使用子查询或作用域(如下)。

can :update, Project, ["priority < ?", 3] do |project|
  project.priority < 3
end

如果您使用 load_resource 并且不提供此 SQL 参数,则不会为索引操作设置实例变量,因为它们无法转换为数据库查询。

From the documentation, this might help... the accessible_by is a class method, and therefore like the index action in this case.

Fetching Records

A block's conditions are only executable through Ruby. If you are Fetching Records using accessible_by it will raise an exception. To fetch records from the database you need to supply an SQL string representing the condition. The SQL will go in the WHERE clause, if you need to do joins consider using sub-queries or scopes (below).

can :update, Project, ["priority < ?", 3] do |project|
  project.priority < 3
end

If you are using load_resource and don't supply this SQL argument, the instance variable will not be set for the index action since they cannot be translated to a database query.

黑色毁心梦 2025-01-10 18:28:38

此代码产生“无法确定 SQL”错误:

 cannot :create, PaymentGateway do |gateway|
   PaymentGateway.valid_gateway? gateway.type
 end 

这解决了问题:

 cannot :create, PaymentGateway, ['type IS NOT NULL'] do |gateway|
   PaymentGateway.valid_gateway? gateway.type
 end

请注意,您可以使用作用域 PaymentGateway.my_scope,而不是 SQL 语句。

This code produces the "The SQL cannot be determined" error:

 cannot :create, PaymentGateway do |gateway|
   PaymentGateway.valid_gateway? gateway.type
 end 

This solved the problem:

 cannot :create, PaymentGateway, ['type IS NOT NULL'] do |gateway|
   PaymentGateway.valid_gateway? gateway.type
 end

Note that you can use a scope PaymentGateway.my_scope, instead of the SQL statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文