molten-trunk/molten5_withgrid/vendor/plugins/acts_as_ferret/lib/class_methods.rb
def retrieve_records(id_arrays, find_options = {})
      result = []
      # get objects for each model
      id_arrays.each do |model, id_array|
        next if id_array.empty?
        begin
          model = model.constantize
        rescue
          raise "Please use ':store_class_name => true' if you want to use multi_search.\n#{$!}"
        end

        # check for include association that might only exist on some models in case of multi_search
        filtered_include_options = []
        if include_options = find_options[:include]
          include_options.each do |include_option|
            filtered_include_options << include_option if model.reflections.has_key?(include_option.is_a?(Hash) ? include_option.keys[0].to_sym : include_option.to_sym)
          end
        end
        filtered_include_options=nil if filtered_include_options.empty?

        # fetch
        tmp_result = nil
        model.send(:with_scope, :find => find_options) do 
          tmp_result = model.find( :all, :conditions => [ 
            "#{model.table_name}.#{model.primary_key} in (?)", id_array.keys ],  
            :include => filtered_include_options ) 
        end

        # set scores and rank
        tmp_result.each do |record|
          record.ferret_rank, record.ferret_score = id_array[record.id.to_s]
        end
        # merge with result array
        result.concat tmp_result
      end
      
      # order results as they were found by ferret, unless an AR :order
      # option was given
      result.sort! { |a, b| a.ferret_rank <=> b.ferret_rank } unless find_options[:order]
      return result
    end
# retrieves search result records from a data structure like this:
# { 'Model1' => { '1' => [ rank, score ], '2' => [ rank, score ] }
#
# TODO: in case of STI AR will filter out hits from other 
# classes for us, but this
# will lead to less results retrieved --> scoping of ferret query
# to self.class is still needed.
# from the ferret ML (thanks Curtis Hatter)
# > I created a method in my base STI class so I can scope my query. For scoping
# > I used something like the following line:
# > 
# > query << " role:#{self.class.eql?(Contents) '*' : self.class}"
# > 
# > Though you could make it more generic by simply asking
# > "self.descends_from_active_record?" which is how rails decides if it should
# > scope your "find" query for STI models. You can check out "base.rb" in
# > activerecord to see that.
# but maybe better do the scoping in find_id_by_contents...


in case of STI AR will filter out hits from other
classes for us, but this
will lead to less results retrieved --> scoping of ferret query
to self.class is still needed.
from the ferret ML (thanks Curtis Hatter)
> I created a method in my base STI class so I can scope my query.
in case of STI AR will filter out hits from other classes for us but this will lead to less results retrieved -- > scoping of ferret query to self . class is still needed . from the ferret ML ( thanks Curtis Hatter ) > I created a method in my base STI class so I can scope my query .
def retrieve_records(id_arrays, find_options = {})
      result = []
      id_arrays.each do |model, id_array|
        next if id_array.empty?
        begin
          model = model.constantize
        rescue
          raise "Please use ':store_class_name => true' if you want to use multi_search.\n#{$!}"
        end
        filtered_include_options = []
        if include_options = find_options[:include]
          include_options.each do |include_option|
            filtered_include_options << include_option if model.reflections.has_key?(include_option.is_a?(Hash) ? include_option.keys[0].to_sym : include_option.to_sym)
          end
        end
        filtered_include_options=nil if filtered_include_options.empty?
        tmp_result = nil
        model.send(:with_scope, :find => find_options) do 
          tmp_result = model.find( :all, :conditions => [ 
            "#{model.table_name}.#{model.primary_key} in (?)", id_array.keys ],  
            :include => filtered_include_options ) 
        end
        tmp_result.each do |record|
          record.ferret_rank, record.ferret_score = id_array[record.id.to_s]
        end
        result.concat tmp_result
      end
      result.sort! { |a, b| a.ferret_rank <=> b.ferret_rank } unless find_options[:order]
      return result
    end
def retrieve_records ( id_arrays , find_options = { } ) result = [ ] id_arrays . each do | model , id_array | next if id_array . empty? begin model = model . constantize rescue raise "Please use ':store_class_name => true' if you want to use multi_search.\n#{$!}" end filtered_include_options = [ ] if include_options = find_options [ :include ] include_options . each do | include_option | filtered_include_options << include_option if model . reflections . has_key? ( include_option . is_a? ( Hash ) ? include_option . keys [ 0 ] . to_sym : include_option . to_sym ) end end filtered_include_options = nil if filtered_include_options . empty? tmp_result = nil model . send ( :with_scope , :find => find_options ) do tmp_result = model . find ( :all , :conditions => [ "#{model.table_name}.#{model.primary_key} in (?)" , id_array . keys ] , :include => filtered_include_options ) end tmp_result . each do | record | record . ferret_rank , record . ferret_score = id_array [ record . id . to_s ] end result . concat tmp_result end result . sort! { | a , b | a . ferret_rank <=> b . ferret_rank } unless find_options [ :order ] return result end
retrieves search result records from a data structure like this:
{ 'Model1' => { '1' => [ rank, score ], '2' => [ rank, score ] }
retrieves search result records from a data structure like this : { ' Model1 ' = > { ' 1 ' = > [ rank score ] ' 2 ' = > [ rank score ] }
# get objects for each model # check for include association that might only exist on some models in case of multi_search # fetch # set scores and rank # merge with result array # order results as they were found by ferret, unless an AR :order # option was given
id_arrays
find_options