提交 bc129f1b 编写于 作者: J Julia López

add missing collection.reload documentation [ci skip]

上级 27f18cc6
...@@ -1192,8 +1192,8 @@ module ClassMethods ...@@ -1192,8 +1192,8 @@ module ClassMethods
# <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>. # <tt>has_many :clients</tt> would add among others <tt>clients.empty?</tt>.
# #
# [collection] # [collection]
# Returns an array of all the associated objects. # Returns a Relation of all the associated objects.
# An empty array is returned if none are found. # An empty Relation is returned if none are found.
# [collection<<(object, ...)] # [collection<<(object, ...)]
# Adds one or more objects to the collection by setting their foreign keys to the collection's primary key. # Adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
# Note that this operation instantly fires update SQL without waiting for the save or update call on the # Note that this operation instantly fires update SQL without waiting for the save or update call on the
...@@ -1250,6 +1250,9 @@ module ClassMethods ...@@ -1250,6 +1250,9 @@ module ClassMethods
# [collection.create!(attributes = {})] # [collection.create!(attributes = {})]
# Does the same as <tt>collection.create</tt>, but raises ActiveRecord::RecordInvalid # Does the same as <tt>collection.create</tt>, but raises ActiveRecord::RecordInvalid
# if the record is invalid. # if the record is invalid.
# [collection.reload]
# Returns a Relation of all of the associated objects, forcing a database read.
# An empty Relation is returned if none are found.
# #
# === Example # === Example
# #
...@@ -1269,6 +1272,7 @@ module ClassMethods ...@@ -1269,6 +1272,7 @@ module ClassMethods
# * <tt>Firm#clients.build</tt> (similar to <tt>Client.new("firm_id" => id)</tt>) # * <tt>Firm#clients.build</tt> (similar to <tt>Client.new("firm_id" => id)</tt>)
# * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new("firm_id" => id); c.save; c</tt>) # * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new("firm_id" => id); c.save; c</tt>)
# * <tt>Firm#clients.create!</tt> (similar to <tt>c = Client.new("firm_id" => id); c.save!</tt>) # * <tt>Firm#clients.create!</tt> (similar to <tt>c = Client.new("firm_id" => id); c.save!</tt>)
# * <tt>Firm#clients.reload</tt>
# The declaration can also include an +options+ hash to specialize the behavior of the association. # The declaration can also include an +options+ hash to specialize the behavior of the association.
# #
# === Scopes # === Scopes
...@@ -1712,8 +1716,8 @@ def belongs_to(name, scope = nil, options = {}) ...@@ -1712,8 +1716,8 @@ def belongs_to(name, scope = nil, options = {})
# <tt>has_and_belongs_to_many :categories</tt> would add among others <tt>categories.empty?</tt>. # <tt>has_and_belongs_to_many :categories</tt> would add among others <tt>categories.empty?</tt>.
# #
# [collection] # [collection]
# Returns an array of all the associated objects. # Returns a Relation of all the associated objects.
# An empty array is returned if none are found. # An empty Relation is returned if none are found.
# [collection<<(object, ...)] # [collection<<(object, ...)]
# Adds one or more objects to the collection by creating associations in the join table # Adds one or more objects to the collection by creating associations in the join table
# (<tt>collection.push</tt> and <tt>collection.concat</tt> are aliases to this method). # (<tt>collection.push</tt> and <tt>collection.concat</tt> are aliases to this method).
...@@ -1751,6 +1755,9 @@ def belongs_to(name, scope = nil, options = {}) ...@@ -1751,6 +1755,9 @@ def belongs_to(name, scope = nil, options = {})
# Returns a new object of the collection type that has been instantiated # Returns a new object of the collection type that has been instantiated
# with +attributes+, linked to this object through the join table, and that has already been # with +attributes+, linked to this object through the join table, and that has already been
# saved (if it passed the validation). # saved (if it passed the validation).
# [collection.reload]
# Returns a Relation of all of the associated objects, forcing a database read.
# An empty Relation is returned if none are found.
# #
# === Example # === Example
# #
...@@ -1769,6 +1776,7 @@ def belongs_to(name, scope = nil, options = {}) ...@@ -1769,6 +1776,7 @@ def belongs_to(name, scope = nil, options = {})
# * <tt>Developer#projects.exists?(...)</tt> # * <tt>Developer#projects.exists?(...)</tt>
# * <tt>Developer#projects.build</tt> (similar to <tt>Project.new("developer_id" => id)</tt>) # * <tt>Developer#projects.build</tt> (similar to <tt>Project.new("developer_id" => id)</tt>)
# * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new("developer_id" => id); c.save; c</tt>) # * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new("developer_id" => id); c.save; c</tt>)
# * <tt>Developer#projects.reload</tt>
# The declaration may include an +options+ hash to specialize the behavior of the association. # The declaration may include an +options+ hash to specialize the behavior of the association.
# #
# === Scopes # === Scopes
......
...@@ -829,6 +829,7 @@ author= ...@@ -829,6 +829,7 @@ author=
build_author build_author
create_author create_author
create_author! create_author!
reload_author
``` ```
NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix. NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix.
...@@ -1180,6 +1181,7 @@ account= ...@@ -1180,6 +1181,7 @@ account=
build_account build_account
create_account create_account
create_account! create_account!
reload_account
``` ```
NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix. NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix.
...@@ -1445,6 +1447,7 @@ When you declare a `has_many` association, the declaring class automatically gai ...@@ -1445,6 +1447,7 @@ When you declare a `has_many` association, the declaring class automatically gai
* `collection.build(attributes = {}, ...)` * `collection.build(attributes = {}, ...)`
* `collection.create(attributes = {})` * `collection.create(attributes = {})`
* `collection.create!(attributes = {})` * `collection.create!(attributes = {})`
* `collection.reload`
In all of these methods, `collection` is replaced with the symbol passed as the first argument to `has_many`, and `collection_singular` is replaced with the singularized version of that symbol. For example, given the declaration: In all of these methods, `collection` is replaced with the symbol passed as the first argument to `has_many`, and `collection_singular` is replaced with the singularized version of that symbol. For example, given the declaration:
...@@ -1473,11 +1476,12 @@ books.exists?(...) ...@@ -1473,11 +1476,12 @@ books.exists?(...)
books.build(attributes = {}, ...) books.build(attributes = {}, ...)
books.create(attributes = {}) books.create(attributes = {})
books.create!(attributes = {}) books.create!(attributes = {})
books.reload
``` ```
##### `collection` ##### `collection`
The `collection` method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array. The `collection` method returns a Relation of all of the associated objects. If there are no associated objects, it returns an empty Relation.
```ruby ```ruby
@books = @author.books @books = @author.books
...@@ -1611,6 +1615,14 @@ The `collection.create` method returns a single or array of new objects of the a ...@@ -1611,6 +1615,14 @@ The `collection.create` method returns a single or array of new objects of the a
Does the same as `collection.create` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid. Does the same as `collection.create` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
##### `collection.reload`
The `collection.reload` method returns a Relation of all of the associated objects, forcing a database read. If there are no associated objects, it returns an empty Relation.
```ruby
@books = @author.books.reload
```
#### Options for `has_many` #### Options for `has_many`
While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this association uses two such options: While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this association uses two such options:
...@@ -1967,6 +1979,7 @@ When you declare a `has_and_belongs_to_many` association, the declaring class au ...@@ -1967,6 +1979,7 @@ When you declare a `has_and_belongs_to_many` association, the declaring class au
* `collection.build(attributes = {})` * `collection.build(attributes = {})`
* `collection.create(attributes = {})` * `collection.create(attributes = {})`
* `collection.create!(attributes = {})` * `collection.create!(attributes = {})`
* `collection.reload`
In all of these methods, `collection` is replaced with the symbol passed as the first argument to `has_and_belongs_to_many`, and `collection_singular` is replaced with the singularized version of that symbol. For example, given the declaration: In all of these methods, `collection` is replaced with the symbol passed as the first argument to `has_and_belongs_to_many`, and `collection_singular` is replaced with the singularized version of that symbol. For example, given the declaration:
...@@ -1995,6 +2008,7 @@ assemblies.exists?(...) ...@@ -1995,6 +2008,7 @@ assemblies.exists?(...)
assemblies.build(attributes = {}, ...) assemblies.build(attributes = {}, ...)
assemblies.create(attributes = {}) assemblies.create(attributes = {})
assemblies.create!(attributes = {}) assemblies.create!(attributes = {})
assemblies.reload
``` ```
##### Additional Column Methods ##### Additional Column Methods
...@@ -2006,7 +2020,7 @@ WARNING: The use of extra attributes on the join table in a `has_and_belongs_to_ ...@@ -2006,7 +2020,7 @@ WARNING: The use of extra attributes on the join table in a `has_and_belongs_to_
##### `collection` ##### `collection`
The `collection` method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array. The `collection` method returns a Relation of all of the associated objects. If there are no associated objects, it returns an empty Relation.
```ruby ```ruby
@assemblies = @part.assemblies @assemblies = @part.assemblies
...@@ -2118,6 +2132,14 @@ The `collection.create` method returns a new object of the associated type. This ...@@ -2118,6 +2132,14 @@ The `collection.create` method returns a new object of the associated type. This
Does the same as `collection.create`, but raises `ActiveRecord::RecordInvalid` if the record is invalid. Does the same as `collection.create`, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
##### `collection.reload`
The `collection.reload` method returns a Relation of all of the associated objects, forcing a database read. If there are no associated objects, it returns an empty Relation.
```ruby
@assemblies = @part.assemblies.reload
```
#### Options for `has_and_belongs_to_many` #### Options for `has_and_belongs_to_many`
While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_and_belongs_to_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this association uses two such options: While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_and_belongs_to_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this association uses two such options:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册