@@ -349,13 +349,32 @@ def association_instance_set(name, association)
349
349
#
350
350
# The project class now has the following methods (and more) to ease the traversal and
351
351
# manipulation of its relationships:
352
- # * <tt>Project#portfolio</tt>, <tt>Project#portfolio=(portfolio)</tt>, <tt>Project#reload_portfolio</tt>
353
- # * <tt>Project#project_manager</tt>, <tt>Project#project_manager=(project_manager)</tt>, <tt>Project#reload_project_manager</tt>
354
- # * <tt>Project#milestones.empty?</tt>, <tt>Project#milestones.size</tt>, <tt>Project#milestones</tt>, <tt>Project#milestones<<(milestone)</tt>,
355
- # <tt>Project#milestones.delete(milestone)</tt>, <tt>Project#milestones.destroy(milestone)</tt>, <tt>Project#milestones.find(milestone_id)</tt>,
356
- # <tt>Project#milestones.build</tt>, <tt>Project#milestones.create</tt>
357
- # * <tt>Project#categories.empty?</tt>, <tt>Project#categories.size</tt>, <tt>Project#categories</tt>, <tt>Project#categories<<(category1)</tt>,
358
- # <tt>Project#categories.delete(category1)</tt>, <tt>Project#categories.destroy(category1)</tt>
352
+ #
353
+ # project = Project.first
354
+ # project.portfolio
355
+ # project.portfolio = Portfolio.first
356
+ # project.reload_portfolio
357
+ #
358
+ # project.project_manager
359
+ # project.project_manager = ProjectManager.first
360
+ # project.reload_project_manager
361
+ #
362
+ # project.milestones.empty?
363
+ # project.milestones.size
364
+ # project.milestones
365
+ # project.milestones << Milestone.first
366
+ # project.milestones.delete(Milestone.first)
367
+ # project.milestones.destroy(Milestone.first)
368
+ # project.milestones.find(Milestone.first.id)
369
+ # project.milestones.build
370
+ # project.milestones.create
371
+ #
372
+ # project.categories.empty?
373
+ # project.categories.size
374
+ # project.categories
375
+ # project.categories << Category.first
376
+ # project.categories.delete(category1)
377
+ # project.categories.destroy(category1)
359
378
#
360
379
# === A word of warning
361
380
#
@@ -1303,23 +1322,32 @@ module ClassMethods
1303
1322
#
1304
1323
# === Example
1305
1324
#
1306
- # A <tt>Firm</tt> class declares <tt>has_many :clients</tt>, which will add:
1307
- # * <tt>Firm#clients</tt> (similar to <tt>Client.where(firm_id: id)</tt>)
1308
- # * <tt>Firm#clients<<</tt>
1309
- # * <tt>Firm#clients.delete</tt>
1310
- # * <tt>Firm#clients.destroy</tt>
1311
- # * <tt>Firm#clients=</tt>
1312
- # * <tt>Firm#client_ids</tt>
1313
- # * <tt>Firm#client_ids=</tt>
1314
- # * <tt>Firm#clients.clear</tt>
1315
- # * <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>)
1316
- # * <tt>Firm#clients.size</tt> (similar to <tt>Client.count "firm_id = #{id}"</tt>)
1317
- # * <tt>Firm#clients.find</tt> (similar to <tt>Client.where(firm_id: id).find(id)</tt>)
1318
- # * <tt>Firm#clients.exists?(name: 'ACME')</tt> (similar to <tt>Client.exists?(name: 'ACME', firm_id: firm.id)</tt>)
1319
- # * <tt>Firm#clients.build</tt> (similar to <tt>Client.new(firm_id: id)</tt>)
1320
- # * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new(firm_id: id); c.save; c</tt>)
1321
- # * <tt>Firm#clients.create!</tt> (similar to <tt>c = Client.new(firm_id: id); c.save!</tt>)
1322
- # * <tt>Firm#clients.reload</tt>
1325
+ # class Firm < ActiveRecord::Base
1326
+ # has_many :clients
1327
+ # end
1328
+ #
1329
+ # Declaring <tt>has_many :clients</tt> adds the following methods (and more):
1330
+ #
1331
+ # firm = Firm.find(2)
1332
+ # client = Client.find(6)
1333
+ #
1334
+ # firm.clients # similar to Client.where(firm_id: 2)
1335
+ # firm.clients << client
1336
+ # firm.clients.delete(client)
1337
+ # firm.clients.destroy(client)
1338
+ # firm.clients = [client]
1339
+ # firm.client_ids
1340
+ # firm.client_ids = [6]
1341
+ # firm.clients.clear
1342
+ # firm.clients.empty? # similar to firm.clients.size == 0
1343
+ # firm.clients.size # similar to Client.count "firm_id = 2"
1344
+ # firm.clients.find # similar to Client.where(firm_id: 2).find(6)
1345
+ # firm.clients.exists?(name: 'ACME') # similar to Client.exists?(name: 'ACME', firm_id: 2)
1346
+ # firm.clients.build # similar to Client.new(firm_id: 2)
1347
+ # firm.clients.create # similar to Client.create(firm_id: 2)
1348
+ # firm.clients.create! # similar to Client.create!(firm_id: 2)
1349
+ # firm.clients.reload
1350
+ #
1323
1351
# The declaration can also include an +options+ hash to specialize the behavior of the association.
1324
1352
#
1325
1353
# === Scopes
@@ -1506,14 +1534,22 @@ def has_many(name, scope = nil, **options, &extension)
1506
1534
#
1507
1535
# === Example
1508
1536
#
1509
- # An Account class declares <tt>has_one :beneficiary</tt>, which will add:
1510
- # * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.where(account_id: id).first</tt>)
1511
- # * <tt>Account#beneficiary=(beneficiary)</tt> (similar to <tt>beneficiary.account_id = account.id; beneficiary.save</tt>)
1512
- # * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new(account_id: id)</tt>)
1513
- # * <tt>Account#create_beneficiary</tt> (similar to <tt>b = Beneficiary.new(account_id: id); b.save; b</tt>)
1514
- # * <tt>Account#create_beneficiary!</tt> (similar to <tt>b = Beneficiary.new(account_id: id); b.save!; b</tt>)
1515
- # * <tt>Account#reload_beneficiary</tt>
1516
- # * <tt>Account#reset_beneficiary</tt>
1537
+ # class Account < ActiveRecord::Base
1538
+ # has_one :beneficiary
1539
+ # end
1540
+ #
1541
+ # Declaring <tt>has_one :beneficiary</tt> adds the following methods (and more):
1542
+ #
1543
+ # account = Account.find(5)
1544
+ # beneficiary = Beneficiary.find(8)
1545
+ #
1546
+ # account.beneficiary # similar to Beneficiary.find_by(account_id: 5)
1547
+ # account.beneficiary = beneficiary # similar to beneficiary.update(account_id: 5)
1548
+ # account.build_beneficiary # similar to Beneficiary.new(account_id: 5)
1549
+ # account.create_beneficiary # similar to Beneficiary.create(account_id: 5)
1550
+ # account.create_beneficiary! # similar to Beneficiary.create!(account_id: 5)
1551
+ # account.reload_beneficiary
1552
+ # account.reset_beneficiary
1517
1553
#
1518
1554
# === Scopes
1519
1555
#
@@ -1682,16 +1718,24 @@ def has_one(name, scope = nil, **options)
1682
1718
#
1683
1719
# === Example
1684
1720
#
1685
- # A Post class declares <tt>belongs_to :author</tt>, which will add:
1686
- # * <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
1687
- # * <tt>Post#author=(author)</tt> (similar to <tt>post.author_id = author.id</tt>)
1688
- # * <tt>Post#build_author</tt> (similar to <tt>post.author = Author.new</tt>)
1689
- # * <tt>Post#create_author</tt> (similar to <tt>post.author = Author.new; post.author.save; post.author</tt>)
1690
- # * <tt>Post#create_author!</tt> (similar to <tt>post.author = Author.new; post.author.save!; post.author</tt>)
1691
- # * <tt>Post#reload_author</tt>
1692
- # * <tt>Post#reset_author</tt>
1693
- # * <tt>Post#author_changed?</tt>
1694
- # * <tt>Post#author_previously_changed?</tt>
1721
+ # class Post < ActiveRecord::Base
1722
+ # belongs_to :author
1723
+ # end
1724
+ #
1725
+ # Declaring <tt>belongs_to :author</tt> adds the following methods (and more):
1726
+ #
1727
+ # post = Post.find(7)
1728
+ # author = Author.find(19)
1729
+ #
1730
+ # post.author # similar to Author.find(post.author_id)
1731
+ # post.author = author # similar to post.author_id = author.id
1732
+ # post.build_author # similar to post.author = Author.new
1733
+ # post.create_author # similar to post.author = Author.new; post.author.save; post.author
1734
+ # post.create_author! # similar to post.author = Author.new; post.author.save!; post.author
1735
+ # post.reload_author
1736
+ # post.reset_author
1737
+ # post.author_changed?
1738
+ # post.author_previously_changed?
1695
1739
#
1696
1740
# === Scopes
1697
1741
#
@@ -1888,22 +1932,31 @@ def belongs_to(name, scope = nil, **options)
1888
1932
#
1889
1933
# === Example
1890
1934
#
1891
- # A Developer class declares <tt>has_and_belongs_to_many :projects</tt>, which will add:
1892
- # * <tt>Developer#projects</tt>
1893
- # * <tt>Developer#projects<<</tt>
1894
- # * <tt>Developer#projects.delete</tt>
1895
- # * <tt>Developer#projects.destroy</tt>
1896
- # * <tt>Developer#projects=</tt>
1897
- # * <tt>Developer#project_ids</tt>
1898
- # * <tt>Developer#project_ids=</tt>
1899
- # * <tt>Developer#projects.clear</tt>
1900
- # * <tt>Developer#projects.empty?</tt>
1901
- # * <tt>Developer#projects.size</tt>
1902
- # * <tt>Developer#projects.find(id)</tt>
1903
- # * <tt>Developer#projects.exists?(...)</tt>
1904
- # * <tt>Developer#projects.build</tt> (similar to <tt>Project.new(developer_id: id)</tt>)
1905
- # * <tt>Developer#projects.create</tt> (similar to <tt>c = Project.new(developer_id: id); c.save; c</tt>)
1906
- # * <tt>Developer#projects.reload</tt>
1935
+ # class Developer < ActiveRecord::Base
1936
+ # has_and_belongs_to_many :projects
1937
+ # end
1938
+ #
1939
+ # Declaring <tt>has_and_belongs_to_many :projects</tt> adds the following methods (and more):
1940
+ #
1941
+ # developer = Developer.find(11)
1942
+ # project = Project.find(9)
1943
+ #
1944
+ # developer.projects
1945
+ # developer.projects << project
1946
+ # developer.projects.delete(project)
1947
+ # developer.projects.destroy(project)
1948
+ # developer.projects = [project]
1949
+ # developer.project_ids
1950
+ # developer.project_ids = [9]
1951
+ # developer.projects.clear
1952
+ # developer.projects.empty?
1953
+ # developer.projects.size
1954
+ # developer.projects.find(9)
1955
+ # developer.projects.exists?(9)
1956
+ # developer.projects.build # similar to Project.new(developer_id: 11)
1957
+ # developer.projects.create # similar to Project.create(developer_id: 11)
1958
+ # developer.projects.reload
1959
+ #
1907
1960
# The declaration may include an +options+ hash to specialize the behavior of the association.
1908
1961
#
1909
1962
# === Scopes
0 commit comments