I use Sunspot to index and search for multiple models in a Rails project, and I need to limit the results based on the associations of the HABTM models with the Department model. This is because users may not have permission to view records in all departments, so the results of these departments should not be returned.
Here are the important parts of the two models:
class Message < ActiveRecord::Base has_many :comments, dependent: :destroy has_and_belongs_to_many :departments searchable do text :title, :body text :comments do comments.map(&:body) end date :created_at integer :department_ids, using: :department_ids, references: Department, multiple: true end end class Document < ActiveRecord::Base has_and_belongs_to_many :departments searchable do text :name date :created_at integer :department_ids, using: :department_ids, references: Department, multiple: true end end
And here is the code of the search controller:
class SearchController < ApplicationController def index
The problem is that the user can read documents in departments A and B, but they should only see messages in department B.
Is there a way to apply the with scope to a specific model in multi-model search? Or is there another way to do this that I am missing?
ruby-on-rails sunspot sunspot- solr sunspot-rails
Simon
source share