Skip to content

Commit 082ffa9

Browse files
authored
Merge pull request rails#50342 from JohnAnon9771/doc/improve-controlling-association-scope
doc(active_record): adding more examples about scope control
2 parents 26ec5e5 + c430cc3 commit 082ffa9

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

guides/source/association_basics.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,30 @@ module MyApplication
793793
end
794794
```
795795

796-
This will work fine, because both the `Supplier` and the `Account` class are defined within the same scope. But the following will _not_ work, because `Supplier` and `Account` are defined in different scopes:
796+
This will work fine, because both the `Supplier` and the `Account` class are defined within the same scope (`MyApplication::Business`). This organization allows structuring models into folders based on their scope, without having to explicitly add the scope to every association:
797+
798+
```ruby
799+
# app/models/my_application/business/supplier.rb
800+
module MyApplication
801+
module Business
802+
class Supplier < ApplicationRecord
803+
has_one :account
804+
end
805+
end
806+
end
807+
# app/models/my_application/business/account.rb
808+
module MyApplication
809+
module Business
810+
class Account < ApplicationRecord
811+
belongs_to :supplier
812+
end
813+
end
814+
end
815+
```
816+
817+
It is crucial to note that this does not affect the naming of your tables. For instance, if there is a `MyApplication::Business::Supplier` model, there must also be a `my_application_business_suppliers` table.
818+
819+
Note that he following will _not_ work, because `Supplier` and `Account` are defined in different scopes (`MyApplication::Business` and `MyApplication::Billing`):
797820

798821
```ruby
799822
module MyApplication

0 commit comments

Comments
 (0)