铁轨 +思维-Sphinx多态关联

发布于 2024-10-04 11:49:49 字数 902 浏览 1 评论 0原文

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. 问题:我无法按类型搜索(始终为空数组)。一个错误?我希望获得关注者类型为“AAA”的所有用户。

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

  2. 问题:为什么我必须反转关联才能获得正确的结果(索引定义): 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 
  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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追风人 2024-10-11 11:49:49

关于第一个问题 - 字符串过滤器在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文