Skip to content

Commit

Permalink
Changelog and doc updates for the previous changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Jul 27, 2012
1 parent d109954 commit 3205c76
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
24 changes: 21 additions & 3 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,24 @@
## Rails 4.0.0 (unreleased) ##

* `Model.all` now returns an `ActiveRecord::Relation`, rather than an
array of records. Use `Model.to_a` or `Relation#to_a` if you really
want an array.

In some specific cases, this may cause breakage when upgrading.
However in most cases the `ActiveRecord::Relation` will just act as a
lazy-loaded array and there will be no problems.

Note that calling `Model.all` with options (e.g.
`Model.all(conditions: '...')` was already deprecated, but it will
still return an array in order to make the transition easier.

`Model.scoped` is deprecated in favour of `Model.all`.

`Relation#all` still returns an array, but is deprecated (since it
would serve no purpose if we made it return a `Relation`).

*Jon Leighton*

* Deprecate `update_column` method in favor of `update_columns`.

*Rafael Mendonça França*
Expand Down Expand Up @@ -249,13 +268,12 @@

Note that as an interim step, it is possible to rewrite the above as:

Post.scoped(:where => { :comments_count => 10 }, :limit => 5)
Post.all.merge(:where => { :comments_count => 10 }, :limit => 5)

This could save you a lot of work if there is a lot of old-style
finder usage in your application.

Calling `Post.scoped(options)` is a shortcut for
`Post.scoped.merge(options)`. `Relation#merge` now accepts a hash of
`Relation#merge` now accepts a hash of
options, but they must be identical to the names of the equivalent
finder method. These are mostly identical to the old-style finder
option names, except in the following cases:
Expand Down
12 changes: 6 additions & 6 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -129,7 +129,7 @@ def references!(*args)
#
# First: takes a block so it can be used just like Array#select.
#
# Model.scoped.select { |m| m.field == value }
# Model.all.select { |m| m.field == value }
#
# This will build an array of objects from the database for the scope,
# converting them into an array and iterating through them using Array#select.
Expand Down Expand Up @@ -503,7 +503,7 @@ def create_with(value)
# Like #create_with but modifies the relation in place. Raises
# +ImmutableRelation+ if the relation has already been loaded.
#
# users = User.scoped.create_with!(name: 'Oscar')
# users = User.all.create_with!(name: 'Oscar')
# users.new.name # => 'Oscar'
def create_with!(value)
self.create_with_value = value ? create_with_value.merge(value) : {}
Expand Down Expand Up @@ -566,16 +566,16 @@ def uniq!(value = true)
# end
# end
#
# scope = Model.scoped.extending(Pagination)
# scope = Model.all.extending(Pagination)
# scope.page(params[:page])
#
# You can also pass a list of modules:
#
# scope = Model.scoped.extending(Pagination, SomethingElse)
# scope = Model.all.extending(Pagination, SomethingElse)
#
# === Using a block
#
# scope = Model.scoped.extending do
# scope = Model.all.extending do
# def page(number)
# # pagination code goes here
# end
Expand All @@ -584,7 +584,7 @@ def uniq!(value = true)
#
# You can also use a block and a module list:
#
# scope = Model.scoped.extending(Pagination) do
# scope = Model.all.extending(Pagination) do
# def per_page(number)
# # pagination code goes here
# end
Expand Down
15 changes: 0 additions & 15 deletions guides/source/active_record_querying.textile
Expand Up @@ -1132,21 +1132,6 @@ Using a class method is the preferred way to accept arguments for scopes. These
category.posts.created_before(time)
</ruby>

h4. Working with scopes

Where a relational object is required, the +scoped+ method may come in handy. This will return an +ActiveRecord::Relation+ object which can have further scoping applied to it afterwards. A place where this may come in handy is on associations

<ruby>
client = Client.find_by_first_name("Ryan")
orders = client.orders.scoped
</ruby>

With this new +orders+ object, we are able to ascertain that this object can have more scopes applied to it. For instance, if we wanted to return orders only in the last 30 days at a later point.

<ruby>
orders.where("created_at > ?", 30.days.ago)
</ruby>

h4. Applying a default scope

If we wish for a scope to be applied across all queries to the model we can use the +default_scope+ method within the model itself.
Expand Down

0 comments on commit 3205c76

Please sign in to comment.