-->

Rails 4.1 - thinking-sphinx association not workin

2019-06-11 02:39发布

问题:

I have a Rails app that contains two models, Post and User, I'm using Sphinx and the thinking-sphinx gem to enable search. Each post belongs to a user, with belongs_to, and each user can have many posts. When I search for posts I also want to be able to search by the user's name that have created the post. My index for Posts looks like this:

ThinkingSphinx::Index.define :post, :with => :active_record do
  indexes name, :sortable => true
  indexes post_description
  indexes user.name, as: :post_user

  has user_id, team_id, created_at, updated_at
end

My user index looks like this:

ThinkingSphinx::Index.define :user, :with => :active_record do
  indexes name, :sortable => true
  indexes email
  indexes about

  has team_id, created_at, updated_at
end

When I run: rake ts:index, I get the following error:

rake aborted!
NoMethodError: undefined method `_reflect_on_association' for #<Class:0x007fd417610c90>

Any ideas on how I can solove it?

Update

I'm pretty sure that it's this line that causes he indexes user.name, as: :post_user, when I don't get any errors. But the association still doesn't work..

回答1:

This is due to an update in the joiner gem (version 0.3.2), which requires Rails >= 4.1.2.

See this commit: https://github.com/pat/joiner/commit/de1403ec85d21dabf21a715ae962ee31c6ffe8d1

Either update to Rails 4.1.2 or add the following to your Gemfile:

gem 'joiner', '0.3.1'