xpepermint
xpepermint

Reputation: 36273

Rails + Thinking-Sphinx polymorphic association

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!

Upvotes: 2

Views: 896

Answers (1)

pat
pat

Reputation: 16226

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.

Upvotes: 2

Related Questions