On 22/08/2009, at 4:24 AM, gobigdave wrote: > Two problems I still have, however. > > 1. Would rather search like User.first.contacts.search.
See my other email in this thread. Although actually... if you're searching on Contacts, not Users, shouldn't the define_index block exist for the Contact model, not the User model? The STI relationships makes this all a bit tricky, mind you. > 2. With a many-to-many relationship where there are additional > attributes on the association model, how do I filter by those > attributes. For example, in my example the Relationship model has a > created_at attribute. It would be great to sort on when the > relationship was created, but I can't get it to work. Is this > possible? The tricky part about this is that you'll have multiple created_at values for a single record. If you can somehow use a single value (either using custom SQL attributes), then sorting should work. Perhaps: has 'MAX(relationships.created_at)', :as => :created_at, :type => :datetime > On Aug 21, 8:30 pm, gobigdave <[email protected]> wrote: >> In far more simple terms, is there a way I can do: >> >> User.first.contacts.search('dave') >> >> That would be ideal, but I haven't been able to find the right >> indexes/ >> has combinations. >> >> On Aug 21, 5:17 pm, gobigdave <[email protected]> wrote: >> >> >> >>> 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 -~----------~----~----~----~------~----~------~--~---
