@@ -457,6 +457,9 @@ class ShardRecord < ApplicationRecord
457
457
shard_two: { writing: :primary_shard_two, reading: :primary_shard_two_replica }
458
458
}
459
459
end
460
+
461
+ class Person < ShardRecord
462
+ end
460
463
` ` `
461
464
462
465
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
472
475
using sharding, both a `role` and a `shard` must be passed:
473
476
474
477
```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
477
480
end
478
481
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 ".
482
485
end
483
486
```
484
487
485
488
The horizontal sharding API also supports read replicas. You can swap the
486
489
role and the shard with the ` connected_to ` API.
487
490
488
491
``` ruby
489
- ActiveRecord :: Base .connected_to(role: :reading , shard: :shard_one ) do
492
+ ShardRecord .connected_to(role: :reading , shard: :shard_one ) do
490
493
Person .first # Lookup record from read replica of shard one.
491
494
end
492
495
```
0 commit comments