Skip to content

use translation model's connection pool#677

Open
ollym wants to merge 3 commits intoshioyama:masterfrom
ollym:allow-customisable-parent_class
Open

use translation model's connection pool#677
ollym wants to merge 3 commits intoshioyama:masterfrom
ollym:allow-customisable-parent_class

Conversation

@ollym
Copy link

@ollym ollym commented Aug 28, 2025

For situations where you might want to leverage Rails' multiple databases, eg.

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to database: { writing: :primary, reading: :primary_replica }
end
class Word < ApplicationRecord
  extend Mobility
  translates :name, :meaning
end

Currently if you do this:

ApplicationRecord.connected_to(role: :reading) do
  Word.first.name
end

The word record is fetched using the reading role, but the translation is fetched using the writing role because the Translation class inherits from it's own abstract class.

This PR introduces some logic that will use by default the abstract class of the model that it is translating, and therefore reuse its connection properties.

@ollym ollym changed the title allow customisable parent_class use translation model's connection pool Sep 3, 2025
@ollym
Copy link
Author

ollym commented Sep 23, 2025

@shioyama any chance of getting this merged? This fixes how it works with Rails' multiple databases. Rails internally adopts the same mechanism for habtm tables here: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb#L45-L47

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant