Skip to content

Commit c430cc3

Browse files
committed
doc(active_record): adding more examples about scope control
1 parent 6828546 commit c430cc3

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
@@ -788,7 +788,30 @@ module MyApplication
788788
end
789789
```
790790

791-
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:
791+
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:
792+
793+
```ruby
794+
# app/models/my_application/business/supplier.rb
795+
module MyApplication
796+
module Business
797+
class Supplier < ApplicationRecord
798+
has_one :account
799+
end
800+
end
801+
end
802+
# app/models/my_application/business/account.rb
803+
module MyApplication
804+
module Business
805+
class Account < ApplicationRecord
806+
belongs_to :supplier
807+
end
808+
end
809+
end
810+
```
811+
812+
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.
813+
814+
Note that he following will _not_ work, because `Supplier` and `Account` are defined in different scopes (`MyApplication::Business` and `MyApplication::Billing`):
792815

793816
```ruby
794817
module MyApplication

0 commit comments

Comments
 (0)