【问题标题】:How to deny default scope for ActiveRecord entity如何拒绝 ActiveRecord 实体的默认范围
【发布时间】:2012-01-16 15:04:37
【问题描述】:

我为我的 ActiveRecord 实体创建了几个范围,我想强制用户使用其中之一。如果没有选择命名范围,有没有办法抛出异常。 即,我想禁止这样做:

Entity.all

但允许这样做:

Entity.user(current_user)

谢谢

【问题讨论】:

  • 你是用户,不是吗?有一些纪律:-)
  • 是的,我是,所以我想保护我免于射中我的腿:)

标签: ruby-on-rails activerecord named-scope


【解决方案1】:

覆盖 :all 范围

scope :all, #something like throwing an exception or calling other query

【讨论】:

  • 我怀疑 current_user 是 Entity 的一个类方法,但是通过类似 sentient_user gem 的东西,那将是scope :all, Entity.user(User.current)
  • 你是对的,但想法还是一样,覆盖 :all scope
  • scope :all, lambda{Entity.user(User.current)} 在通话时进行评估,我的第一个也是错误的,(仍然通过 sentient_user)。是的,这个想法成立。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
相关资源
最近更新 更多