Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models using different database #22

Closed
westonganger opened this issue Mar 19, 2015 · 7 comments

Comments

Projects
None yet
2 participants
@westonganger
Copy link

commented Mar 19, 2015

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.

@mrkamel

This comment has been minimized.

Copy link
Owner

commented Mar 19, 2015

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

@westonganger

This comment has been minimized.

Copy link
Author

commented Mar 19, 2015

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.

@mrkamel

This comment has been minimized.

Copy link
Owner

commented Mar 19, 2015

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

@westonganger

This comment has been minimized.

Copy link
Author

commented Mar 19, 2015

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.

@mrkamel

This comment has been minimized.

Copy link
Owner

commented Mar 19, 2015

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.

@westonganger

This comment has been minimized.

Copy link
Author

commented Mar 19, 2015

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

@mrkamel

This comment has been minimized.

Copy link
Owner

commented Mar 19, 2015

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).

@mrkamel mrkamel closed this Mar 19, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.