@@ -1502,6 +1502,10 @@ module ClassMethods
1502
1502
# [:ensuring_owner_was]
1503
1503
# Specifies an instance method to be called on the owner. The method must return true in order for the
1504
1504
# 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.
1505
1509
#
1506
1510
# Option examples:
1507
1511
# has_many :comments, -> { order("posted_on") }
@@ -1514,6 +1518,7 @@ module ClassMethods
1514
1518
# has_many :subscribers, through: :subscriptions, source: :user
1515
1519
# has_many :subscribers, through: :subscriptions, disable_joins: true
1516
1520
# has_many :comments, strict_loading: true
1521
+ # has_many :comments, query_constraints: [:blog_id, :post_id]
1517
1522
def has_many ( name , scope = nil , **options , &extension )
1518
1523
reflection = Builder ::HasMany . build ( self , name , scope , options , &extension )
1519
1524
Reflection . add_reflection self , name , reflection
@@ -1681,6 +1686,10 @@ def has_many(name, scope = nil, **options, &extension)
1681
1686
# [:ensuring_owner_was]
1682
1687
# Specifies an instance method to be called on the owner. The method must return true in order for the
1683
1688
# 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.
1684
1693
#
1685
1694
# Option examples:
1686
1695
# has_one :credit_card, dependent: :destroy # destroys the associated credit card
@@ -1695,6 +1704,7 @@ def has_many(name, scope = nil, **options, &extension)
1695
1704
# has_one :primary_address, -> { where(primary: true) }, through: :addressables, source: :addressable
1696
1705
# has_one :credit_card, required: true
1697
1706
# has_one :credit_card, strict_loading: true
1707
+ # has_one :employment_record_book, query_constraints: [:organization_id, :employee_id]
1698
1708
def has_one ( name , scope = nil , **options )
1699
1709
reflection = Builder ::HasOne . build ( self , name , scope , options )
1700
1710
Reflection . add_reflection self , name , reflection
@@ -1853,6 +1863,10 @@ def has_one(name, scope = nil, **options)
1853
1863
# [:ensuring_owner_was]
1854
1864
# Specifies an instance method to be called on the owner. The method must return true in order for the
1855
1865
# 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.
1856
1870
#
1857
1871
# Option examples:
1858
1872
# belongs_to :firm, foreign_key: "client_of"
@@ -1868,6 +1882,7 @@ def has_one(name, scope = nil, **options)
1868
1882
# belongs_to :user, optional: true
1869
1883
# belongs_to :account, default: -> { company.account }
1870
1884
# belongs_to :account, strict_loading: true
1885
+ # belong_to :note, query_constraints: [:organization_id, :note_id]
1871
1886
def belongs_to ( name , scope = nil , **options )
1872
1887
reflection = Builder ::BelongsTo . build ( self , name , scope , options )
1873
1888
Reflection . add_reflection self , name , reflection
0 commit comments