Skip to content

Commit 20df382

Browse files
authored
Merge pull request rails#49154 from Shopify/query-constraints-option-in-associations
Document `query_constraints` option in associations
2 parents 7716e32 + 96d3cca commit 20df382

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,10 @@ module ClassMethods
15021502
# [:ensuring_owner_was]
15031503
# Specifies an instance method to be called on the owner. The method must return true in order for the
15041504
# associated records to be deleted in a background job.
1505+
# [:query_constraints]
1506+
# Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1507+
# This is an optional option. By default Rails will attempt to derive the value automatically.
1508+
# When the value is set the Array size must match associated model's primary key or `query_constraints` size.
15051509
#
15061510
# Option examples:
15071511
# has_many :comments, -> { order("posted_on") }
@@ -1514,6 +1518,7 @@ module ClassMethods
15141518
# has_many :subscribers, through: :subscriptions, source: :user
15151519
# has_many :subscribers, through: :subscriptions, disable_joins: true
15161520
# has_many :comments, strict_loading: true
1521+
# has_many :comments, query_constraints: [:blog_id, :post_id]
15171522
def has_many(name, scope = nil, **options, &extension)
15181523
reflection = Builder::HasMany.build(self, name, scope, options, &extension)
15191524
Reflection.add_reflection self, name, reflection
@@ -1681,6 +1686,10 @@ def has_many(name, scope = nil, **options, &extension)
16811686
# [:ensuring_owner_was]
16821687
# Specifies an instance method to be called on the owner. The method must return true in order for the
16831688
# associated records to be deleted in a background job.
1689+
# [:query_constraints]
1690+
# Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1691+
# This is an optional option. By default Rails will attempt to derive the value automatically.
1692+
# When the value is set the Array size must match associated model's primary key or `query_constraints` size.
16841693
#
16851694
# Option examples:
16861695
# has_one :credit_card, dependent: :destroy # destroys the associated credit card
@@ -1695,6 +1704,7 @@ def has_many(name, scope = nil, **options, &extension)
16951704
# has_one :primary_address, -> { where(primary: true) }, through: :addressables, source: :addressable
16961705
# has_one :credit_card, required: true
16971706
# has_one :credit_card, strict_loading: true
1707+
# has_one :employment_record_book, query_constraints: [:organization_id, :employee_id]
16981708
def has_one(name, scope = nil, **options)
16991709
reflection = Builder::HasOne.build(self, name, scope, options)
17001710
Reflection.add_reflection self, name, reflection
@@ -1853,6 +1863,10 @@ def has_one(name, scope = nil, **options)
18531863
# [:ensuring_owner_was]
18541864
# Specifies an instance method to be called on the owner. The method must return true in order for the
18551865
# associated records to be deleted in a background job.
1866+
# [:query_constraints]
1867+
# Serves as a composite foreign key. Defines the list of columns to be used to query the associated object.
1868+
# This is an optional option. By default Rails will attempt to derive the value automatically.
1869+
# When the value is set the Array size must match associated model's primary key or `query_constraints` size.
18561870
#
18571871
# Option examples:
18581872
# belongs_to :firm, foreign_key: "client_of"
@@ -1868,6 +1882,7 @@ def has_one(name, scope = nil, **options)
18681882
# belongs_to :user, optional: true
18691883
# belongs_to :account, default: -> { company.account }
18701884
# belongs_to :account, strict_loading: true
1885+
# belong_to :note, query_constraints: [:organization_id, :note_id]
18711886
def belongs_to(name, scope = nil, **options)
18721887
reflection = Builder::BelongsTo.build(self, name, scope, options)
18731888
Reflection.add_reflection self, name, reflection

0 commit comments

Comments
 (0)