Module: ThinkingSphinx::ActiveRecord::HasManyAssociation

Defined in:
lib/thinking_sphinx/active_record/has_many_association.rb

Instance Method Summary collapse

Instance Method Details

#search(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/thinking_sphinx/active_record/has_many_association.rb', line 4

def search(*args)
  foreign_key = @reflection.primary_key_name
  stack = [@reflection.options[:through]].compact
  
  attribute   = nil
  @reflection.klass.define_indexes
  (@reflection.klass.sphinx_indexes || []).each do |index|
    attribute = index.attributes.detect { |attrib|
      attrib.columns.length == 1 &&
      attrib.columns.first.__name  == foreign_key.to_sym
    }
    break if attribute
  end
  
  raise "Missing Attribute for Foreign Key #{foreign_key}" unless attribute
  
  options = args.extract_options!
  options[:with] ||= {}
  options[:with][attribute.unique_name] = @owner.id
  
  args << options
  @reflection.klass.search(*args)
end