前提
現在、rails6のマルチDB機能によって2つのデータベースを連携してwebサービスの運用をしております。
以下データベース名を
- main
- sub
とします。
参考サイトにあるように、モデルに対してtable_name_prefixを使用することで複数DB間検索を実装しました。
https://github.com/activerecord-hackery/ransack/issues/463#issuecomment-61716552
複数DB間検索の例
mainにitemsというテーブル(main.items)、subにusersというテーブル(sub.users)が存在し、以下のようなモデルがあるとします。
ruby
1class Item < ApplicationRecord 2 belongs_to :user 3end 4 5class User < SubApplicationRecord 6 has_many :items 7end
このとき、ransackでitemに紐づくuserの名前を検索するようなことを指します。
ちなみにこのときのransackのparamsは以下のようになります
item_user_name_cont
発生している問題
ローカルでは複数DB間検索ができていましたが、ステージング環境にデプロイした際には検索時に以下のエラーが発生しました。
Api::V1::Mypage::TestController::get_error_log_str No table named sub.users exists. .../vendor/bundle/ruby/2.6.0/gems/ransack-2.4.2/lib/ransack/adapters/active_record/context.rb:19:in `type_for' .../vendor/bundle/ruby/2.6.0/gems/ransack-2.4.2/lib/ransack/nodes/attribute.rb:35:in `type' .../vendor/bundle/ruby/2.6.0/gems/ransack-2.4.2/lib/ransack/nodes/condition.rb:227:in `block in casted_values_for_attribute' .../vendor/bundle/ruby/2.6.0/gems/ransack-2.4.2/lib/ransack/nodes/condition.rb:227:in `map' .../vendor/bundle/ruby/2.6.0/gems/ransack-2.4.2/lib/ransack/nodes/condition.rb:227:in `casted_values_for_attribute' .../vendor/bundle/ruby/2.6.0/gems/ransack-2.4.2/lib/ransack/nodes/condition.rb:231:in `formatted_values_for_attribute' .../vendor/bundle/ruby/2.6.0/gems/ransack-2.4.2/lib/ransack/adapters/active_record/ransack/nodes/condition.rb:32:in `format_predicate' ...
しかし、sub.usersは存在します。
また、該当のエラーは以下の schema_cache.data_source_exists? が false になることで発生します。
https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/adapters/active_record/context.rb#L19
def type_for(attr) return nil unless attr && attr.valid? name = attr.arel_attribute.name.to_s table = attr.arel_attribute.relation.table_name schema_cache = self.klass.connection.schema_cache unless schema_cache.send(:data_source_exists?, table) raise "No table named #{table} exists." end attr.klass.columns.find { |column| column.name == name }.type end
ローカルにおいてこのエラーは発生しなかったので、railsの設定が間違っているかとも考えましたが、原因を掴めませんでした。
以下が、railsのステージング環境の設定ファイルです。
# environments/staging.rb Rails.application.configure do config.cache_classes = true config.eager_load = true # Full error reports are disabled and caching is turned on. config.consider_all_requests_local = false config.action_controller.perform_caching = true config.read_encrypted_secrets = true config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? config.log_level = :debug config.log_tags = [:request_id] config.action_mailer.perform_caching = false config.i18n.fallbacks = true config.active_support.deprecation = :notify config.log_formatter = ::Logger::Formatter.new if ENV["RAILS_LOG_TO_STDOUT"].present? logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) else config.logger = Logger.new('../log/staging.log', 'daily') end config.active_record.dump_schema_after_migration = false config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.default_url_options = { host: 'xxxxxx' } ActionMailer::Base.smtp_settings = { ... } config.active_storage.service = :xxxx Rails.application.routes.default_url_options[:host] = 'xxxx' end
期待する結果
ステージング環境でも複数DB間検索ができるようにする。
こちらrailsに詳しい方がおられましたら教えていただけると幸いです。
あなたの回答
tips
プレビュー