尝试在所有模型上定义named_scopes和其他ActiveRecord关系

发布于 2024-07-25 10:01:54 字数 1288 浏览 5 评论 0原文

我正在尝试为Rails 应用程序中的所有模型定义一个named_scope。

目前,我已经能够通过为 ActiveRecord::Base 编写一个初始化程序并将常规方法放入其中来接近这一点。 当然,这在创建查询链时并没有提供真正的优势,并且可能是完成工作的最不符合轨道的方式。

但是,当我开始尝试使用 has_many、named_scope 等 ActiveRecord 方法时,它不起作用。

虽然我知道我的named_scope可能不正确,但我真的只需要帮助定义named_scope。 另外,我目前对任何 Ruby ACL GEM 都不感兴趣。

在初始化程序/中:

class ActiveRecord::Base

  has_many(:permissions)
  named_scope(:acl_check, lambda do |user_id, method|
        {
            :include => :permission,
            :conditions => [
                ["permissions.user_id=?", user_id],
                ["permissions.method=?", method],
                ["permissions.classname=?", self.class.name]
            ]
        }
  end)

    # Conducts a permission check for the current instance.
    def check_acl?(user_id, method)

        # Perform the permission check by User.
        permission_check = Permission.find_by_user_id_and_instance_id_and_classname_and_method(user_id, self.id, self.class.name, method)
        if(permission_check)
            # If the row exists, we generate a hit.
            return(true)
        end

        # Perform the permission check by Role.

        # Otherwise, the permissions check was a miss.
        return(false)

    end

end

I'm trying to define a named_scope for all my models in a Rails application.

Presently, I've been able to get close to this by writing an initializer for ActiveRecord::Base and putting regular methods in there.
Of course, this offers no real advantage when it comes to creating query chains and is probably the least rails-ey way of getting the job done.

However, it doesn't work when I start to try and use the has_many, named_scope, etc... ActiveRecord methods.

While I understand my named_scope is likely not correct, I really only want help getting the named_scope defined. Also, I am not interested in any Ruby ACL GEMs at present.

In initializers/:

class ActiveRecord::Base

  has_many(:permissions)
  named_scope(:acl_check, lambda do |user_id, method|
        {
            :include => :permission,
            :conditions => [
                ["permissions.user_id=?", user_id],
                ["permissions.method=?", method],
                ["permissions.classname=?", self.class.name]
            ]
        }
  end)

    # Conducts a permission check for the current instance.
    def check_acl?(user_id, method)

        # Perform the permission check by User.
        permission_check = Permission.find_by_user_id_and_instance_id_and_classname_and_method(user_id, self.id, self.class.name, method)
        if(permission_check)
            # If the row exists, we generate a hit.
            return(true)
        end

        # Perform the permission check by Role.

        # Otherwise, the permissions check was a miss.
        return(false)

    end

end

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

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

发布评论

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

评论(1

耳根太软 2024-08-01 10:01:54

has_many 可能不起作用,因为它是在类主体中评估的,并且预期的外键是针对评估它的类而不是继承类。 (例如,id=42 的 Blog 模型可以有许多使用 blog_id = 42 存储的 Comment 模型,使其工作所需的键基于类名)

如果命名范围正确,则应该可以工作。

继承的方法应该有效。

the has_many will likely not work because it is evaluated in the class body and the expected foreign keys are for the class in which it was evaluated rather than the inheriting classes. (e.g. Blog model with id=42 can have many Comment models stored with blog_id = 42, the keys that are needed to make it work are based on the class name)

The named scope should work if it is correct.

The inherited method should work.

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