Search code examples
ruby-on-railsthinking-sphinx

Using ActiveRecord joins with Thinking Sphinx


When using Thinking Sphinx with a has_one association from class A to B (with B having belongs_to statement), indexing on A, it is possible to use has_many association in index with join statement. This results in a SphinxQL query that groups on primary key of B, of course. However, when I attempt to use the same association with an index on B, I cannot apparently use the association on belongs_to side. My question is, is it possible to use belongs_to association, or is there a workaround? Here are the relevant parts of my code:

class IdentAssociation < ActiveRecord::Base
  has_many :donees, :foreign_key => :ident_id, :class_name => "Donees"
...

class Donees < ActiveRecord::Base
  belongs_to :ident_association, :foreign_key => :ident_id
...

ThinkingSphinx::Index.define :donees, :with => :active_record do
  join ident_association
...

Solution

  • Yes, I think I must have mistyped the name of the association. I also encountered some other problems such as sphinx complaining about missing sphinx_internal_class_name; but that's beyond the scope.