You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, `.connects_to(shards: ...)` coerces all of the shard keys
into symbols. Because an integer cannot be coerced into a symbol,
attempting to identify a shard with an integer will raise an exception:
```ruby
1.to_sym
(irb):1:in '<main>': undefined method 'to_sym' for an instance of Integer (NoMethodError)
```
I think it would be useful to support integers as shard keys along with
symbols. This would simplify shard switching when shards are identified
by integer columns in a database.
As an example, if there is an `accounts` table with a `shard` integer
column which identifies which shard that account's data lives on, this
currently would not work:
```ruby
account = Account.first
ActiveRecord::Base.connected_to(shard: account.shard) do
# Raises NoMethodError
end
```
A workaround would be to first coerce or serialize the integer into a
string but that's unideal:
```ruby
account = Account.first
ActiveRecord::Base.connected_to(shard: account.shard.to_s.to_sym) do
# ...
end
```
This makes a `.connects_to` change, coercing the shard key into a
symbol _unless_ the key is an integer. From there, passing a symbol or
integer to `connected_to(shard: ...)` should both work.
In the test, we call:
```ruby
ActiveRecord::Base.default_shard = 0
```
Normally, it would be `:default`. In that case we'd have to mix symbols
and integers in the `.connects_to:(shards:)` hash which is pretty ugly.
0 commit comments