开发者

Rails + Thinking-Sphinx polymorphic association

开发者 https://www.devze.com 2023-01-26 23:45 出处:网络
class User < ActiveRecord::Base has_many :followings, :as => :followable, :dependent => :destroy, :class_name => \'Follow\'
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.followabl开发者_高级运维e(: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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号