Our rails app has a primary and a replica DB. We already have a method defined which takes a block, and while inside the block, queries will be made against the replica, like so:
on_replica do
@my_things = MyModel.where(my_attr: "my value").to_a
end
This works well in most cases, but if you don't actually perform the query in the block, it doesn't work:
on_replica do
@my_things = MyModel.where(my_attr: "my value")
end
# this will run its query against the primary DB
@my_things.to_a
I'd really like to be able to define a scope (or scope-like thing) so that any query happening past that point in the method chain happens against the replica:
@my_things = MyModel.where(my_attr: "my value").eventually_on_replica
# this runs its query against the replica DB:
@my_things.to_a
Is it possible to define an eventually_on_replica method that would work this way, using the existing on_replica method? Bonus points if MyModel.eventually_on_replica.where(my_attr: "my value") works too.