9

在 ActiveRecord 中有一个default_scope类方法来指定默认范围。例如

class User < ActiveRecord::Base
  default_scope where(:deleted => false)
end

User.all # => SELECT * FROM users WHERE deleted = 0;

我怎样才能做到这一点Sequel::Model

编辑:

经过一番谷歌搜索,我最终找到了一些有用的信息。

class User < Sequel::Model

  # Define some "scopes" (filters on the dataset)
  dataset_module do
    def existing
      filter(deleted: false)
    end

    def active
      filter(disabled: false)
    end
  end

  # This is the equivalent to a default_scope. Set one of the datasets
  # as the default dataset for this model.
  set_dataset(self.active)
end

然后生成的查询如下所示:

User.all # => SELECT * FROM `users` WHERE (`deleted` IS FALSE)

顺便说一句:相当于unscopedunfiltered

User.unfiltered.all # => SELECT * FROM `users`

但是,有一个问题。如果您尝试更新从未过滤数据集获得的用户,它会尝试使用给定数据集更新用户。

User.create(disabled: true, deleted: true)
User.all # => []
u = User.unfiltered.first # => Given user
u.disabled = false
u.save # => UPDATE users SET ... WHERE (disabled IS FALSE AND id = 1)
# => Sequel::NoExistingObject: Attempt to update object did not result in a single row modification

所以我又回到了最初。有什么解决方法吗?

4

1 回答 1

9

最好的解决方法是通过没有默认范围来避免问题。在大多数情况下,默认范围是一个坏主意。如果您希望大多数查询使用范围,则在这些查询中手动应用范围,不要使用默认范围并尝试在其他查询中回退范围。仅当您的所有查询都使用该范围时,默认范围才有意义。

您也可以通过子类化来处理这个问题(用户没有作用域,ActiveUser < 用户有作用域)。但是,我发现显式范围界定方法效果更好。

话虽如此,如果您真的想使用默认范围,以下可能会解决在模型的默认范围之外更新模型实例的问题:

User.instance_dataset.unfiltered!
于 2012-07-26T20:48:02.250 回答