ankane/multiverse

ApplicationRecord being overiden in engine.

Closed this issue · 1 comments

Hey,

I'm having a issue that occurs when using an engine. It seems that it is required to have application_record inherit from the newly created db_record file. My engine has its own application_record oblivious of the fact that it will be setup in a multi database container.

#app/models/x_service_record.rb
class XServiceRecord < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :"x_service_#{Rails.env}"
end
#app/models/x_service/application_record.rb
module XService
  class ApplicationRecord < XServiceRecord
    self.abstract_class = true
    p "root application_record"
  end
end

If this file isn't here, the database isn't recognised.

2.3.1 :006 > XService::Account.first
"engine application_record"
  XService::Account Load (1.0ms)  SELECT  `x_service_accounts`.* FROM `x_service_accounts` ORDER BY `x_service_accounts`.`id` ASC LIMIT 1
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'service_container_development.x_service_accounts' doesn't exist: SELECT  `x_service_accounts`.* FROM `x_service_accounts` ORDER BY `x_service_accounts`.`id` ASC LIMIT 1
  from (irb):6

But having that file there overrides the Engine's application record. The database connects as expected and the following is printed `"root application_record"

#plugins/x_service/app/models/x_service/application_record.rb
module XService
  class ApplicationRecord < ActiveRecord::Base
    self.abstract_class = true
    p "engine application_record"
  end
end

So I don't quite know how to let my Engine's ApplicationRecord inherit from XCatalogRecord.

Hey @iaankrynauw, not sure I completely understand what you're asking. Stack Overflow is a better place for questions.