I have classes like:
class Person < ActiveRecord::Base
has_many :relationships, :class_name => "Relationship", :foreign_key
=> "user_id", :dependent => :delete_all
has_many :owner_relationships, :class_name =>
"Relationship", :foreign_key => "person_id", :dependent => :delete_all
has_many :owners, :through => :owner_relationships, :source => :user
end
class Contact < Person
end
class User < Person
has_many :people, :through => :relationships, :source => :person
has_many :contacts, :through => :relationships, :source
=> :person, :conditions => ["relationships.kind = ?",
Relationship::CONTACT]
has_many :colleagues, :through => :relationships, :source
=> :person, :conditions => ["relationships.kind = ?",
Relationship::COLLEAGUE]
define_index do
has owners(:id), :as => :owners
has owner_relationships.kind, :as => :owners_kind
end
end
class Relationship < ActiveRecord::Base
belongs_to :user, :class_name => "Person", :foreign_key =>
"user_id"
belongs_to :person, :class_name => "Person", :foreign_key =>
"person_id"
end
Essentially, users have contacts and colleagues. Colleagues can only
be other users, but Contacts can be any Person. My problem is that I
can't get data to match up. For example:
user = User.first
user.contacts.count # => 128
but
user = User.first
people = Person.search(:with => { :owners => user.id, :owners_kind =>
Relationship::CONTACT })
people.total_entries # => 140
The search returns people that have COLLEAGUE relationships, and I
can't make it stop. Any idea how I can limit my search to a user's
contacts or colleagues? I tried adding another has_many contact_owners
to Person, and then adding a separate index, but that didn't work
either. Looking at the SQL in the sphinx.conf file, it looks like two
joins are made to the relationships table - once with a kind condition
and one without. Am I doing something wrong, or is there a bug here?
It would be great to be able to do user.contacts.search and have it
work. I tried it but TS complains about a missing user_id foreign key.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en
-~----------~----~----~----~------~----~------~--~---