Rails now includes support for multiple database roles (by default, writing
for the primary and reading
for the replica):
ActiveRecord::Base.connected_to(role: :reading) do
# all code in this block will be connected to the reading role
end
In development, Active Record queries are logged by default, for example:
> User.last
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
How can I include the role used for a query in the logging? For example:
> ActiveRecord::Base.connnected_to(role: :reading) { User.last }
[role: reading] User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]