【问题标题】:thinking-sphinx filtering with belongs_to association带有belongs_to关联的thinking-sphinx过滤
【发布时间】:2015-04-12 15:39:05
【问题描述】:

我有以下模型和索引来使用曲目标题和艺术家姓名搜索曲目:

models/track.rb

class Track < ActiveRecord::Base
  include ThinkingSphinx::Scopes

  belongs_to :artist, :class_name => 'Fan', :foreign_key => 'fan_id'


  sphinx_scope(:of_fan) do |fan_id|
    {with: {fan_id: fan_id}}
  end

  def artist_name
    artist.name
  end
end

models/fan.rb

class Fan < User
  has_many :tracks
end

models/user.rb

class User < ActiveRecord::Base
  #Codes...
end

宝石清单:

gem 'rails', '4.1.0.rc1'
gem 'thinking-sphinx', '~> 3.1.3'

索引/track_index.rb

ThinkingSphinx::Index.define :track, :with => :active_record, :delta => true do
  indexes title

  has plays_count, :as => :play_count
end

我尝试使用以下所有选项重建索引,但没有任何效果并在此块下生成错误:

indexes artist(:first_name), :as => :artist_name
indexes artist.first_name, :as => :artist_name
indexes artist_name
indexes [artist.first_name, artist.last_name], :as => :artist_name

错误:

#For..
#indexes artist(:first_name), :as => :artist_name
#indexes artist.first_name, :as => :artist_name
ERROR: index 'track_delta': sql_range_query: ERROR:  syntax error at or near "AS"
LINE 1: ..."title" AS "title",  AS "author...
                                    ^
 (DSN=pgsql://username:***@localhost:5432/db_name).


#For - indexes [artist.first_name, artist.last_name], :as => :artist_name
rake aborted!
NoMethodError: undefined method `reflections' for nil:NilClass
/Users/morshedalam/.rvm/gems/ruby-2.0.0-p598/gems/joiner-0.2.0/lib/joiner/joins.rb:64:in `reflection_for'
/Users/morshedalam/.rvm/gems/ruby-2.0.0-p598/gems/joiner-0.2.0/lib/joiner/joins.rb:33:in `join_for'
/Users/morshedalam/.rvm/gems/ruby-2.0.0-p598/gems/joiner-0.2.0/lib/joiner/joins.rb:18:in `alias_for'
......

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 thinking-sphinx


    【解决方案1】:

    好的,我找到了解决方案。该问题与 joiner gem 更新有关。将 rails 更新到 4.1.0 并将 joiner gem 添加到 Gemfile 解决了我的问题:

    gem 'joiner', '~> 0.3.4'
    

    后来,我有以下与关联问题相关的帖子:

    Rails 4.1 - thinking-sphinx association not working

    【讨论】:

      【解决方案2】:

      这两个都应该工作:

      indexes artist.first_name, :as => :artist_name
      indexes [artist.first_name, artist.last_name], :as => :artist_name
      

      但这假设您的Fan 模型中有first_name(和last_name)的列 - 是这样吗?

      另外,您使用的是 Thinking Sphinx 和 Rails 的哪个版本?

      【讨论】:

      • 感谢您的回复。我已经编辑了原始帖子以添加宝石信息。我从 github 提交中得到了这个问题。后来,得到了另一篇与我的答案相关的帖子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多