我有一个Rails 5应用程序,并且需要报告功能。我想暂时将其保留在应用程序中,而不使用Rails引擎。报告功能连接到heroku关注者数据库。
我可以复制普通模型并将它们用作父类的子类,该父类按如下方式连接到关注者数据库吗?
module Reporting
class Base < ActiveRecord::Base
self.abstract_class = true
establish_connection("follower_database")
end
end
module Reporting
class User < Reporting::Base
# I would like to avoid copying and pasting all the user model code here and have some fancy way for it to just inherit or included it all so when we add new associations etc. the reporting classes automatically get it..
end
end
答案 0 :(得分:0)
您需要将完整的连接配置传递给establish_connection
方法。要共享普通User类的功能,您可以从该类继承并包括来自模块的数据库连接,例如
module FollowerDatabaseConnection
def self.included(base)
base.establish_connection base.configurations['follower_database']
end
end
class Reporting::User < User
include FollowerDatabaseConnection
end
并在database.yml中定义连接配置
# database.yml
common:
...
development:
...
...
follower_database:
adapter: ..
...