2011-12-20 66 views
2

我想用with(:is_available, true)來過濾太陽黑子的搜索結果。 這是與User模型,但我不能使它與Itinerary模型。如何使用嵌套模型限制太陽黑子搜索?

應用程序/控制器/ 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 

應用/模型/ 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 

應用/模型/ 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 

不限想法?

謝謝!

回答

0

那麼,我真正的問題是索引。

當我更新User型號時,我在控制器中設置了一個標誌(如user_instance.update_index_flag = true)。

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 

就是這樣......

+0

什麼用無操作'inject'聲明瞭?你只是在那裏尋找一個'to_a'? – outoftime 2011-12-20 13:16:17

+0

該死的......是的。好吧,我需要多睡一會兒... – 2011-12-20 13:17:05