@@ -888,6 +888,7 @@ a.first_name == b.writer.first_name # => true
888888* `build_association(attributes = {})`
889889* `create_association(attributes = {})`
890890* `create_association!(attributes = {})`
891+ * `reload_association`
891892
892893这五个方法中的 `association` 要替换成传给 `belongs_to` 方法的第一个参数。对下述声明来说:
893894
@@ -907,6 +908,7 @@ author=
907908build_author
908909create_author
909910create_author!
911+ reload_author
910912----
911913
912914[NOTE]
@@ -924,11 +926,11 @@ create_author!
924926@author = @book.author
925927----
926928
927- 如果关联的对象之前已经取回,会返回缓存版本。如果不想使用缓存版本(强制读取数据库)在父对象上调用 `+#reload +` 方法。
929+ 如果关联的对象之前已经取回,会返回缓存版本。如果不想使用缓存版本(强制读取数据库)在父对象上调用 `+#reload_association +` 方法。
928930
929931[source,ruby]
930932----
931- @author = @book.reload.author
933+ @author = @book.reload_author
932934----
933935
934936[[methods-added-by-belongs-to-association-associate]]
@@ -1065,13 +1067,10 @@ end
10651067[[options-for-belongs-to-dependent]]
10661068====== `:dependent`
10671069
1068- `:dependent` 选项控制属主销毁后怎么处理关联的对象 :
1070+ `:dependent` 选项可以设为以下几个值 :
10691071
1070- * `:destroy`:也销毁关联的对象
1071- * `:delete_all`:直接从数据库中删除关联的对象(不执行回调)
1072- * `:nullify`:把外键设为 `NULL`(不执行回调)
1073- * `:restrict_with_exception`:如果有关联的记录,抛出异常
1074- * `:restrict_with_error`:如果有关联的对象,为属主添加一个错误
1072+ * `:destroy`:销毁对象时,在关联的对象上调用 `destroy` 方法
1073+ * `:delete`:销毁对象时,关联的所有对象直接从数据库中删除,不在关联的对象上调用 `destroy` 方法
10751074
10761075[WARNING]
10771076====
@@ -1283,6 +1282,7 @@ end
12831282* `build_association(attributes = {})`
12841283* `create_association(attributes = {})`
12851284* `create_association!(attributes = {})`
1285+ * `reload_association`
12861286
12871287这五个方法中的 `association` 要替换成传给 `has_one` 方法的第一个参数。对如下的声明来说:
12881288
@@ -1302,6 +1302,7 @@ account=
13021302build_account
13031303create_account
13041304create_account!
1305+ reload_account
13051306----
13061307
13071308[NOTE]
@@ -1319,11 +1320,11 @@ create_account!
13191320@account = @supplier.account
13201321----
13211322
1322- 如果关联的对象之前已经取回,会返回缓存版本。如果不想使用缓存版本,而是强制重新从数据库中读取,在父对象上调用 `#reload ` 方法。
1323+ 如果关联的对象之前已经取回,会返回缓存版本。如果不想使用缓存版本,而是强制重新从数据库中读取,在父对象上调用 `#reload_association ` 方法。
13231324
13241325[source,ruby]
13251326----
1326- @account = @supplier.reload.account
1327+ @account = @supplier.reload_account
13271328----
13281329
13291330[[methods-added-by-has-one-association-associate]]
@@ -1606,6 +1607,7 @@ end
16061607* `collection.build(attributes = {}, ...)`
16071608* `collection.create(attributes = {})`
16081609* `collection.create!(attributes = {})`
1610+ * `collection.reload`
16091611
16101612这些个方法中的 `collection` 要替换成传给 `has_many` 方法的第一个参数。`collection_singular` 要替换成第一个参数的单数形式。对如下的声明来说:
16111613
@@ -1636,12 +1638,13 @@ books.exists?(...)
16361638books.build(attributes = {}, ...)
16371639books.create(attributes = {})
16381640books.create!(attributes = {})
1641+ books.reload
16391642----
16401643
16411644[[methods-added-by-has-many-collection]]
16421645====== `collection`
16431646
1644- `collection` 方法返回一个数组 ,包含所有关联的对象。如果没有关联的对象,则返回空数组 。
1647+ `collection` 方法返回一个 `Relation` 对象 ,包含所有关联的对象。如果没有关联的对象,则返回一个空 `Relation` 对象 。
16451648
16461649[source,ruby]
16471650----
@@ -1795,10 +1798,20 @@ WARNING: 如果设为 `dependent: :destroy`,对象会被删除,这与 `depen
17951798----
17961799
17971800[[methods-added-by-has-many-collection-create-bang-attributes]]
1798- ##### `collection.create!(attributes = {})`
1801+ ====== `collection.create!(attributes = {})`
17991802
18001803作用与 `collection.create` 相同,但如果记录无效,会抛出 `ActiveRecord::RecordInvalid` 异常。
18011804
1805+ [[methods-added-by-has-many-collection-reload]]
1806+ ====== `collection.reload`
1807+
1808+ `collection.reload` 强制从数据库中读取,返回一个 `Relation` 对象,包含所有关联的对象。如果没有关联的对象,返回一个空 `Relation` 对象。
1809+
1810+ [source,ruby]
1811+ ----
1812+ @books = @author.books.reload
1813+ ----
1814+
18021815[[options-for-has-many]]
18031816===== `has_many` 方法的选项
18041817
@@ -2182,6 +2195,7 @@ person.articles << article unless person.articles.include?(article)
21822195* `collection.build(attributes = {})`
21832196* `collection.create(attributes = {})`
21842197* `collection.create!(attributes = {})`
2198+ * `collection.reload`
21852199
21862200这些个方法中的 `collection` 要替换成传给 `has_and_belongs_to_many` 方法的第一个参数。`collection_singular` 要替换成第一个参数的单数形式。对如下的声明来说:
21872201
@@ -2212,6 +2226,7 @@ assemblies.exists?(...)
22122226assemblies.build(attributes = {}, ...)
22132227assemblies.create(attributes = {})
22142228assemblies.create!(attributes = {})
2229+ assemblies.reload
22152230----
22162231
22172232[[additional-column-methods]]
@@ -2224,7 +2239,7 @@ WARNING: 在 `has_and_belongs_to_many` 关联的联结表中使用其他字段
22242239[[methods-added-by-has-and-belongs-to-many-collection]]
22252240====== `collection`
22262241
2227- `collection` 方法返回一个数组 ,包含所有关联的对象。如果没有关联的对象,则返回空数组 。
2242+ `collection` 方法返回一个 `Relation` 对象 ,包含所有关联的对象。如果没有关联的对象,则返回一个空 `Relation` 对象 。
22282243
22292244[source,ruby]
22302245----
@@ -2360,6 +2375,16 @@ NOTE: 这个方法是 `collection.concat` 和 `collection.push` 的别名。
23602375
23612376作用和 `collection.create` 相同,但如果记录无效,会抛出 `ActiveRecord::RecordInvalid` 异常。
23622377
2378+ [[methods-added-by-has-and-belongs-to-many-collection-reload]]
2379+ ====== `collection.reload`
2380+
2381+ `collection.reload` 方法强制从数据库中读取,返回一个 `Relation` 对象,包含所有关联的对象。如果没有关联的对象,返回一个空 `Relation` 对象。
2382+
2383+ [source,ruby]
2384+ ----
2385+ @assemblies = @part.assemblies.reload
2386+ ----
2387+
23632388[[options-for-has-and-belongs-to-many]]
23642389===== `has_and_belongs_to_many` 方法的选项
23652390
0 commit comments