Skip to content

Commit

Permalink
Add a note about Rails 7.2 method overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
dalibor committed Sep 9, 2024
1 parent 97e88af commit 9461c27
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,23 @@ To tell an ActiveRecord model to use the Octoshark connection we can override th

```ruby
class Post < ActiveRecord::Base
def self.connection
CONN_MANAGER.current_connection
end
end
```
class << self
def connection
CONN_MANAGER.current_connection
end

Alternatively, we can extract it as a module and include in multiple models.
# For Rails 7.2+ we need to override the following methods too:

```ruby
module ShardingModel
extend ActiveSupport::Concern
def lease_connection
connection
end

module ClassMethods
def connection
CONN_MANAGER.current_connection
def with_connection
yield connection
end

def schema_cache
connection.schema_cache
end
end
end
Expand Down

0 comments on commit 9461c27

Please sign in to comment.