铁轨 +思维-Sphinx多态关联
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
问题:我无法按类型搜索(始终为空数组)。一个错误?我希望获得关注者类型为“AAA”的所有用户。
User.search '', :with =>; { :follower_type =>; 'AAA' }
问题:为什么我必须反转关联才能获得正确的结果(索引定义): follow.followable(:id), :as =>; :followable_id 而不是 followings.followable(:id), :as =>; :followable_id 我想获取 id=1 用户的关注者列表
User.search :with =>; {:followable_id =>; 1} # id=1的用户的关注者列表
谢谢!
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
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' }
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=1User.search :with => {:followable_id => 1} # List of followers for a user with id=1
Thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于第一个问题 - 字符串过滤器在 Sphinx 中不起作用。这应该在未来改变(随着 Sphinx 1.10-beta,一旦 Thinking Sphinx 支持新功能),但不确定什么时候会发生(我很想尽快说,但不能承诺任何事情)。
不过,有一种解决方法可用......但请记住请注意,您正在处理字符串数组,因此这会增加复杂性。
至于第二个问题,我很难理解数据库的样子(名称令人困惑,但我现在缺乏焦点),所以我暂时先不讨论这个问题。
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.