0

I see no reason why this shouldn't work, but I get no results. A Contact has many OrganizationContacts. And OrganizationContact has a boolean field primary. I've added a filter on this field as shown below.

class Contact < ActiveRecord::Base

  has_many :organization_contacts, :dependent => :destroy

  define_index do
    has organization_contacts(:primary), :as => :primary_contacts

    set_property :delta => true
  end
end

In a debugging session I can see that I do indeed have a Contact with an OrganizationContact that is listed as primary:

(rdb:1) p Contact.first.organization_contacts.first.primary
true

But if I do a ThinkingSphinx search using that filter, I get nothing:

(rdb:1) p Contact.search :with => { :primary_contacts => true }
[]

Can anyone explain?

Samo
  • 8,202
  • 13
  • 58
  • 95

1 Answers1

0

Crickets chirp. Alas, we came up with a work around.

class Contact < ActiveRecord::Base

  has_many :primary_organization_contacts, :class_name => "OrganizationContact", :foreign_key => "contact_id", :conditions => { :primary => true }
  has_many :organization_contacts, :dependent => :destroy

  define_index do
    has primary_organization_contacts(:id), :as => :primary_organization_contacts

    set_property :delta => true
  end
end

Searching for them is the interesting part. If I want records with no primary organization contacts:

Contact.search :with { :primary_organization_contacts => 0 }

If I want records with at least one primary organization contact:

Contact.search :without { :primary_organization_contacts => 0 }

It makes me feel dirty and confused but it does the job.

Samo
  • 8,202
  • 13
  • 58
  • 95