Query views from another database instead of tables

85 Views Asked by At

I want to query a view from ActiveRecord instead of a regular table. Also, the view reside in a different database, i am using octopus gem to access the other database, but the issue is i am unable to query the views.

I tried something like this

class ViewExportDataWell < ExternalRecord
  self.table_name = 'ViewExportDataWells' //this is my view name
end

and trying to access it as

ViewExportDataWell.ransack(params[:q]).result

but this works for a regular table and not a view. Any leads on how I can achieve this would be great, Thanks.

1

There are 1 best solutions below

0
On

you need to connect the model to their corresponding database

class MyModel < ApplicationRecord  
  establish_connection(URI.encode(ENV["OTHER_DB_URI"]))
  self.table_name = "ViewExportDataWells"
end

For more info about establish_connection go to https://apidock.com/rails/ActiveRecord/Base/establish_connection/class