方法
实例公共方法
default_scopes?(all_queries: false) 链接
检查模型是否具有任何默认作用域。如果 all_queries 设置为 true,该方法将检查模型是否具有 all_queries 为 true 的 default_scopes。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/scoping/default.rb, line 62 def default_scopes?(all_queries: false) if all_queries self.default_scopes.any?(&:all_queries) else self.default_scopes.any? end end
unscoped(&block) 链接
返回模型的作用域,不包含之前设置的作用域。
class Post < ActiveRecord::Base belongs_to :user def self.default_scope where(published: true) end end class User < ActiveRecord::Base has_many :posts end Post.all # Fires "SELECT * FROM posts WHERE published = true" Post.unscoped.all # Fires "SELECT * FROM posts" Post.where(published: false).unscoped.all # Fires "SELECT * FROM posts" User.find(1).posts # Fires "SELECT * FROM posts WHERE published = true AND posts.user_id = 1" User.find(1).posts.unscoped # Fires "SELECT * FROM posts"
此方法还接受一个块。块内的所有查询将不使用之前设置的作用域。
Post.unscoped { Post.limit(10) # Fires "SELECT * FROM posts LIMIT 10" }
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/scoping/default.rb, line 50 def unscoped(&block) block_given? ? relation.scoping(&block) : relation end
实例私有方法
default_scope(scope = nil, all_queries: nil, &block) 链接
在您的模型中使用此宏为模型的所有操作设置默认作用域。
class Article < ActiveRecord::Base default_scope { where(published: true) } end Article.all # SELECT * FROM articles WHERE published = true
default_scope 也在创建/构建记录时应用。在更新或删除记录时,它不会被应用。
Article.new.published # => true Article.create.published # => true
要在更新或删除记录时应用 default_scope,请添加 all_queries: true。
class Article < ActiveRecord::Base default_scope -> { where(blog_id: 1) }, all_queries: true end
将默认作用域应用于所有查询将确保记录始终根据附加条件进行查询。请注意,只有 where 子句适用,因为在返回单个主键对象的查询中添加 order 没有意义。
Article.find(1).destroy # DELETE ... FROM `articles` where ID = 1 AND blog_id = 1;
(您也可以将任何响应 call 的对象传递给 default_scope 宏,当构建默认作用域时,它将被调用。)
如果您在模型中使用多个 default_scope 声明,它们将合并在一起。
class Article < ActiveRecord::Base default_scope { where(published: true) } default_scope { where(rating: 'G') } end Article.all # SELECT * FROM articles WHERE published = true AND rating = 'G'
继承和模块包含也是如此,其中父级或模块定义了一个 default_scope,而子级或包含类定义了第二个。
如果您需要对默认作用域执行更复杂的操作,您可以选择将其定义为类方法。
class Article < ActiveRecord::Base def self.default_scope # Should return a scope, you can call 'super' here etc. end end
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/scoping/default.rb, line 129 def default_scope(scope = nil, all_queries: nil, &block) # :doc: scope = block if block_given? if scope.is_a?(Relation) || !scope.respond_to?(:call) raise ArgumentError, "Support for calling #default_scope without a block is removed. For example instead " \ "of `default_scope where(color: 'red')`, please use " \ "`default_scope { where(color: 'red') }`. (Alternatively you can just redefine " \ "self.default_scope.)" end default_scope = DefaultScope.new(scope, all_queries) self.default_scopes += [default_scope] end