mrkamel/search_cop

Models using different database

westonganger opened this issue · 7 comments

Basically I have a site model which is in a different master database.

I have the following search in an associated model:

attributes :name, :description, site: ["site.site_code", "site.name"]

Whats happening is that the search query is generated using the fields in the master database. But the table join is is looking for the sites table on the application database.

I don't fully understand. You use multiple db's? If yes, how do you want search cop to work in this scenario?

Well hopefully join using the database/table that the associated model uses. Is this doable with this or have I surpassed the scope of this gem.

I still don't fully understand what you try to achive. Can you provide more details? Schema, Model, what lives in which db? Thanks

class SharedModels < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "master_#{Rails.env}"
end         

class Site < SharedModels
  has_many :nodes
end

class Node < ActiveRecord::Base
  belongs_to :site

  search_scope :search do 
    attributes :name, :description, site: ["site.site_code", "site.name"]
  end
end

Node is in application database, Site is in master database. Im performing the search on node.

Ah, ok, thanks. However, cross db join's are not possible SQL wise. As SearchCop operates on the SQL level, this won't work, sorry.

great thanks for your help, any tips on implementing this?

Search both models individually

@nodes = Node.search(params[:q_nodes])
@sites = Site.search(params[:q_sites])...

and "merge" the results application wise or add a single data source and index/denormalize your data there to search for both at the same time (ElasticSearch, Solr, Sphinx).