如何使用嵌套模型限制太阳黑子搜索? [英] How to restrict Sunspot search with nested models?
本文介绍了如何使用嵌套模型限制太阳黑子搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 with(:is_available, true)
过滤 Sunspot 搜索结果.这适用于 User
模型,但我无法使其与 Itinerary
模型一起使用.
I want to filter the Sunspot search results with with(:is_available, true)
.
This is working with the User
model, but I can't make it work with the Itinerary
model.
app/controllers/search_controller.rb:
app/controllers/search_controller.rb:
class SearchController < ApplicationController
before_filter :fulltext_actions
private
def fulltext_actions
@itineraries = do_fulltext_search(Itinerary)
@users = do_fulltext_search(User)
@itineraries_size = @itineraries.size
@users_size = @users.size
end
def do_fulltext_search(model)
Sunspot.search(model) do
with(:is_available, true)
fulltext params[:search]
end.results
end
end
app/models/user.rb:
app/models/user.rb:
class User < ActiveRecord::Base
has_many :itineraries, :dependent => :destroy
searchable do
text :first_name, :boost => 3
text :last_name, :boost => 3
text :status
boolean :is_available, :using => :available?
end
def available?
!self.suspended
end
end
app/models/itinerary.rb:
app/models/itinerary.rb:
class Itinerary < ActiveRecord::Base
belongs_to :user
searchable do
text :title, :boost => 3
text :budget
text :description
boolean :is_available, :using => :available?
end
def available?
!self.user.suspended
end
end
有什么想法吗?
谢谢!
推荐答案
嗯,我真正的问题是索引.
Well, my real problem was the indexation.
当我更新 User
模型时,我在我的控制器中设置了一个标志(如 user_instance.update_index_flag = true
).
When I update the User
model, I set a flag (like user_instance.update_index_flag = true
) in my controller.
在 User
模型中:
attr_accessor :update_index_flag
after_save :reindex_sunspot
def reindex_sunspot
if self.update_index_flag
Sunspot.index(self.itineraries.to_a)
end
end
就是这样...
这篇关于如何使用嵌套模型限制太阳黑子搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文