Skip to content

Commit b0e8b5a

Browse files
committed
Update examples in horizontal sharding to avoid confusion
Via global connection setting, so that we avoid `ActiveRecord::ConnectionNotDefined` errors when trying to set a shard globally that not all DB configurations have, and avoid breaking other models that might connect to different DBs and shards.
1 parent 31aec23 commit b0e8b5a

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

guides/source/active_record_multiple_databases.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ class ShardRecord < ApplicationRecord
457457
shard_two: { writing: :primary_shard_two, reading: :primary_shard_two_replica }
458458
}
459459
end
460+
461+
class Person < ShardRecord
462+
end
460463
```
461464

462465
If you're using shards, make sure both `migrations_paths` and `schema_dump` remain unchanged for
@@ -472,21 +475,21 @@ Then models can swap shards manually via the `connected_to` API. If
472475
using sharding, both a `role` and a `shard` must be passed:
473476
474477
```ruby
475-
ActiveRecord::Base.connected_to(role: :writing, shard: :default) do
476-
@id = Person.create! # Creates a record in shard named ":default"
478+
ShardRecord.connected_to(role: :writing, shard: :shard_one) do
479+
@person = Person.create! # Creates a record in shard shard_one
477480
end
478481
479-
ActiveRecord::Base.connected_to(role: :writing, shard: :shard_one) do
480-
Person.find(@id) # Can't find record, doesn't exist because it was created
481-
# in the shard named ":default".
482+
ShardRecord.connected_to(role: :writing, shard: :shard_two) do
483+
Person.find(@person.id) # Can't find record, doesn't exist because it was created
484+
# in the shard named ":shard_one".
482485
end
483486
```
484487

485488
The horizontal sharding API also supports read replicas. You can swap the
486489
role and the shard with the `connected_to` API.
487490

488491
```ruby
489-
ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one) do
492+
ShardRecord.connected_to(role: :reading, shard: :shard_one) do
490493
Person.first # Lookup record from read replica of shard one.
491494
end
492495
```

0 commit comments

Comments
 (0)