morshed
morshed

Reputation: 31

thinking-sphinx filtering with belongs_to association

I have following models and indices to search track using track title and artist name:

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 list:

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

indices/track_index.rb

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

  has plays_count, :as => :play_count
end

I've tried to rebuild indexing using all options below but nothing worked and generates errors under this block:

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

Errors:

#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'
......

Any help would be appreciated.

Upvotes: 1

Views: 240

Answers (2)

morshed
morshed

Reputation: 31

Ok, I've got the solution. The issue was related to joiner gem update. Updating rails to 4.1.0 and adding joiner gem to Gemfile solved my issue:

gem 'joiner', '~> 0.3.4'

Later, I've got following post related to association issue:

Rails 4.1 - thinking-sphinx association not working

Upvotes: 1

pat
pat

Reputation: 16226

Both of these should work:

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

But this presumes there are columns in your Fan model for first_name (and last_name) - is this the case?

Also, which versions of Thinking Sphinx and Rails are you using?

Upvotes: 0

Related Questions