Rails 现在包括对多个数据库角色的支持(默认情况下,writing
主要和reading
副本):
ActiveRecord::Base.connected_to(role: :reading) do
# all code in this block will be connected to the reading role
end
在开发中,默认情况下会记录 Active Record 查询,例如:
> User.last
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT ? [["LIMIT", 1]]
如何在日志记录中包含用于查询的角色?例如:
> 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]]