1

We have a users table. Users have many listings.

We'd like to shard the association model Listing such that all users stay on database "master" shard. Users will get a shard_id column and listings will be split into different databases "shard1", "shard2".

We can augment our code to access the listings on the correct shard using the using method:

Listing.where(user: current_user).using(current_user.shard_id)

However that is a big code change. Ideally we want to just keep using our existing association statements like this:

current_user.listings

And have it automatically use current_user.shard_id beneath the hood.

Any suggestions for doing this?

Homan
  • 25,618
  • 22
  • 70
  • 107
  • Did you ever find a solution for this when using associations on different shards without needing to update each and every use of the chained association in the code base? – Streamline Aug 18 '18 at 19:46

1 Answers1

0

According to the documentation, current_user.listings should work out of the box.

Octopus also handles associations. When you try to get a object that is associated to another object, you could use normal ActiveRecord syntax to get the objects

https://github.com/thiagopradi/octopus/wiki/How-Octopus-Works

machineboy2045
  • 349
  • 4
  • 4
  • That only works if current_user and listings are on the same shard. It doesn't work if User is on master shard and Listing is on Shard1. current_user will assume the association is on the same shard as the current_user. – Homan May 18 '16 at 18:06