使用Sunspot查询具有不同属性的多个模型
我正在使用Sunspot来索引和搜索Rails项目中的几个模型,我需要根据模型的HABTM与Department
模型的关联来限制结果。 这是因为用户可能没有权限查看所有部门的记录,因此不应返回这些部门的结果。
以下是两个模型的重要部分:
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
这是搜索控制器代码:
class SearchController < ApplicationController def index # These arrays are created here for the sake of this example document_permitted_departments = [1, 2, 3] message_permitted_departments = [3, 4] search = Sunspot.search Document, Message do # This obviously doesn't work with(:department_ids, document_permitted_departments) with(:department_ids, message_permitted_departments) fulltext params[:q] paginate page: params[:page], per_page: SEARCH_RESULTS_PER_PAGE order_by :created_at, :desc end @results = search.results @number_of_results = search.total respond_to do |format| format.js format.html end end end
问题是用户可能能够读取部门A和部门B中的文档,但他们应该只能看到部门B中的消息。
有没有办法将with
范围应用于多模型搜索中的特定模型? 还是有另外一种方法可以解决这个问题吗?
经过更多的谷歌搜索和一些反复试验,我终于弄明白了。 这是我最终得到的代码的大量注释版本:
class SearchController < ApplicationController before_filter :authenticate_user! def index # These arrays are created here for the sake of this example # Push 0 on to the end because empty arrays break the `with :department_ids` scopes below document_permitted_departments = [1, 2, 3].push(0) message_permitted_departments = [3, 4].push(0) search = Sunspot.search Document, Message do any_of do # Return anything that matches any of the scopes in this block all_of do # Return only those results that match these scopes with :class, Document # This limits scopes in this block to Document results with :department_ids, document_permitted_departments end all_of do # Return only those results that match these scopes with :class, Message # This limits scopes in this block to Message results with :department_ids, message_permitted_departments end end fulltext params[:q] paginate page: params[:page], per_page: SEARCH_RESULTS_PER_PAGE order_by :created_at, :desc end @results = search.results @number_of_results = search.total respond_to do |format| format.js # index.js.erb format.html # index.html.erb end end end
添加到Simon回答,
我找到了另一个场景。 您希望在一个模型中应用条件并仅使用全文匹配其他模型。
我的第一次尝试就是这个
def search_all(options) Sunspot.search(Post, Tag) do any_of do all_of do with :class, Post with(:status, :published) end end fulltext options[:search] group(:class) { limit 30 } end end
这只给了我Post但不是Tag,不知何故状态::发布应用于Tag并且Tag没有结果。 我也尝试了其他一些变化。
最后我想出了一个解决方案。
Sunspot.search(Post, Tag) do any_of do all_of do with :class, Post with(:status, :published) end # This might look weiered to put all_of without any filter, However # without the next all_of block, status: :published is applied in tags and no results appear for tags. # Meaning any_of is ignored if there is one nested block and Post all_of is applied for entire filter. all_of do with :class, Tag end end fulltext options[:search] group(:class) { limit 30 } end
该解决方案有效。 可能是这可能有其他解决方案。 我仍然对此很满意。
- 更新Rails中的每个Array-Object值
- 为什么Ruby只在我的系统上抛出Segmentation故障,而且只在这个Rails应用程序中?
- 有没有办法使用ruby gem aws-s3从s3下载文件?
- 为什么我的范围被缓存?
- 使用自定义URL(例如http:// localhost:3000 / education_informations / new?user_id = 10)在新页面上呈现错误
- 无效的gemspec(Ruby on Rails)
- 用户has_many:用户,:通过=>:朋友 – 怎么样?
- Rails 3:如何添加user_id – current_user.id配置?
- RVM ruby安装问题