方法
實例公開方法
default_scopes?(all_queries: false) 連結
檢查模型是否具有任何預設範圍。如果 all_queries 設為 true,方法將檢查是否有 any default_scopes 對於模型,其中 all_queries
是 true。
來源: 顯示 | 在 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 子句會套用,因為對透過主鍵傳回單一物件的查詢加入順序毫無意義。
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
,而子級或包含類別定義了第二個 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