2
class User < ActiveRecord::Base
  has_many :followings, :as => :followable, :dependent => :destroy, :class_name => 'Follow'
  has_many :follows, :as => :follower, :dependent => :destroy

  define_index do
    has follows.followable(:id), :as => :followable_id
    has followings.follower(:id), :as => :follower_id
    has follows.followable(:type), :as => :followable_type
    has followings.follower(:type), :as => :follower_type
  end
end 
  1. question: I can not search by type (always empty array). A bug? I would like to get all users where followers are of type 'AAA'.

    User.search '', :with => { :follower_type => 'AAA' }

  2. question: Why do I have to inverse my association to get the right result (index definition): follows.followable(:id), :as => :followable_id instead of followings.followable(:id), :as => :followable_id I would like to get a list of followers for a user with id=1

    User.search :with => {:followable_id => 1} # List of followers for a user with id=1

Thx!

xpepermint
  • 35,055
  • 30
  • 109
  • 163

1 Answers1

2

With regards to the first question - string filters don't work in Sphinx. This should change in the future (with Sphinx 1.10-beta, once Thinking Sphinx supports the new features), but not sure when that'll happen (I'd love to say soon, but can't promise anything).

There is a workaround available, though... but keep in mind you're handling an array of strings, so that's an additional level of complexity.

As for the second question, struggling to get my head around what the database is looking like (confusing names, but I'm lacking focus right now), so I'll just leave it at this for the moment.

pat
  • 16,116
  • 5
  • 40
  • 46
  • @pat Thx for your answare! For the database please see this plugin: https://github.com/xpepermint/acts_as_followable. The model is copied from there (for the second question). – xpepermint Nov 27 '10 at 11:04
  • @pat Also first question should have "has 'CRC32(follows.followable_type)', :as => :followable_type, :type => :integer" index definition right? Not working. – xpepermint Nov 27 '10 at 11:30
  • 1
    Try `:type => :multi` - you're dealing with an array of integers. And filter should be `:with => {:followable_type => 'AAA'.to_crc32}`. – pat Nov 27 '10 at 12:37
  • Actually, you'll need to concatenate all the CRC'd values into a comma-separated string. Are you using PostgreSQL? Or MySQL? – pat Nov 27 '10 at 12:54
  • @pat :multi is working. Thx! Btw my first question is still a strange behaviour. Am I right? – xpepermint Nov 28 '10 at 14:45
  • I know it's been a while, but I think for question 2 is the expected behaviour... you want users who are following User 1. Those users will have User 1 in their follows collection, so using follows.followable(:id) seems to be correct, from what I understand. – pat Dec 28 '10 at 11:58