# Feature proposal: Add a \`shard\_keys\` method to ActiveRecord models

**URL:** <https://discuss.rubyonrails.org/t/feature-proposal-add-a-shard-keys-method-to-activerecord-models/84819>\
**Category:** rubyonrails-core\
**Created:** [February 6, 2024, 4:11pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-a-shard-keys-method-to-activerecord-models/84819 "2024-02-06T16:11:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nonster](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/nonster/32/11251_2.png) [@Nonster](https://discuss.rubyonrails.org/u/Nonster)\
**Post date:** [February 6, 2024, 4:11pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-a-shard-keys-method-to-activerecord-models/84819/1 "2024-02-06T16:11:31Z")

</div>

This is mostly a replica of a [previous discussion from 2020](https://discuss.rubyonrails.org/t/feature-proposal-define-a-shards-method-for-activerecord-abstract-classes/76624) but hopefully it’s okay to re-open the topic.

It would be useful to have a `shard_keys` (could be called `shards` or `shard_names`) method that returns an array of shards that model can connect to. This would also allow for methods like `on_all_shards` (to perform an action on each shard) and `sharded?` (which would return true if the model is configured to connect to shards, otherwise returns false).

I have a working branch that implements all of the above but, given the previous discussion and general guidance to start a topic first, I’m bringing it up here.

The implementation (on `connection_handling`) looks like so:

```ruby
def connects_to(database: {}, shards: {})
  ...
  @shard_keys = shards.keys
  ...
end

def sharded?
  shard_keys.any?
end

def shard_keys
  connection_class_for_self.instance_variable_get(:@shard_keys) || []
end

```

`shard_keys` gets the ivar from `connection_class_for_self` so that all descendants of abstract connection classes know which shards they can connect to. `connection_class_for_self` also returns `self`, for connection classes themselves, and thus would return their own `@shard_keys` ivar.

---

<div class="post-metadata">

**Author:** ![fatkodima](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/fatkodima/32/1380_2.png) [@fatkodima](https://discuss.rubyonrails.org/u/fatkodima)\
**Post date:** [February 8, 2024, 12:57am UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-a-shard-keys-method-to-activerecord-models/84819/2 "2024-02-08T00:57:49Z")

</div>

If you have a patch, please do open a PR. It is easier to get feedback with the code in place.

---

<div class="post-metadata">

**Author:** ![Nonster](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/nonster/32/11251_2.png) [@Nonster](https://discuss.rubyonrails.org/u/Nonster)\
**Post date:** [February 8, 2024, 9:38am UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-a-shard-keys-method-to-activerecord-models/84819/3 "2024-02-08T09:38:09Z")

</div>

Here you go! [Add `.shard_keys`, `.sharded?`, & `.on_all_shards` methods to AR Models by HeyNonster · Pull Request #51009 · rails/rails · GitHub](https://github.com/rails/rails/pull/51009)

One `actioncable integration (3.3)rake test:integration` task failed but it looks unrelated?

---

<div class="post-metadata">

**Author:** ![Nonster](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/nonster/32/11251_2.png) [@Nonster](https://discuss.rubyonrails.org/u/Nonster)\
**Post date:** [March 13, 2024, 3:28pm UTC](https://discuss.rubyonrails.org/t/feature-proposal-add-a-shard-keys-method-to-activerecord-models/84819/4 "2024-03-13T15:28:12Z")

</div>

I’ve rebased the branch to fix some failures due to test fixture changes.

One thing to note, if it’s helpful, we’re using (almost) this exact code in production at Zendesk and it works well for us!
