method
with_scope
v1.0.0 -
Show latest stable
- Class:
ActiveRecord::Base
with_scope(method_scoping = {})public
Scope parameters to method calls within the block. Takes a hash of method_name => parameters hash. method_name may be :find or :create. :find parameters may include the :conditions, :joins, :offset, :limit, and :readonly options. :create parameters are an attributes hash.
Article.with_scope(:find => { :conditions => "blog_id = 1" }, :create => { :blog_id => 1 }) do Article.find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1 a = Article.create(1) a.blog_id == 1 end
2Notes
:find takes more keys than written
The documentation says that the :find keywords "may include the :conditions, :joins, :include, :offset, :limit, and :readonly options". Note that this does not mean that only those options are supported. :sort also works like it should, for example.
Rails 3.2 / 4.x with_scope replacement
If you're looking for with_scope replacement for newer versions, see http://apidock.com/rails/ActiveRecord/Relation/scoping