instance method
connecting_to
Ruby on Rails edge
Since v6.1.7.10Signature
connecting_to(role: default_role, shard: default_shard, prevent_writes: role == ActiveRecord.reading_role)
Use a specified connection to a role and/or shard. The prevent_writes option can be passed to block writes on a connection; it defaults to true when using the reading role.
This method is useful for ensuring that a specific connection is being used. For example, when booting a console in readonly mode.
It is not recommended to use this method in a request since it does not yield to a block like connected_to.
Parameters
-
rolekey = default_role -
shardkey = default_shard -
prevent_writeskey = role == ActiveRecord.reading_role
Source
# File activerecord/lib/active_record/connection_handling.rb, line 219
def connecting_to(role: default_role, shard: default_shard, prevent_writes: role == ActiveRecord.reading_role)
if role == ActiveRecord.reading_role && !prevent_writes
raise ArgumentError, "cannot set `prevent_writes` to false when `role` is `reading`."
end
append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
end
Defined in activerecord/lib/active_record/connection_handling.rb line 219
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionHandling