Getting "undefined method" with Thinking Sphinx scopes when using STI
Asked Answered
H

1

6

I'm using Thinking Sphinx 2.0.13 with Rails 3.2.9.

Given I have and STI class that looks like this:

class User < ActiveRecord::Base
  define_index do
    has :account_id
    has :is_deleted
  end

  sphinx_scope(:by_account) do |account_id|
    {:with => {:account_id => account_id}}
  end

  sphinx_scope(:without_deleted) do
    {:with => {:is_deleted => false}}
  end
end


class Admin < User
end

If I attempt to use a single scope on either the User or Admin class, all is fine. I can also chain scopes together using the User model, as expected. The problem is, If I chain scopes on the Admin model, I get:

> Admin.by_account(1).without_deleted

NoMethodError:   Sphinx Query (2.9ms)  
  Sphinx  Found 3 results
  Admin Load (0.6ms)  SELECT `users`.* FROM `users` WHERE `users`.`type` IN ('Admin') AND `users`.`id` IN (7, 8, 9)
undefined method `without_deleted' for #<ThinkingSphinx::Search:0x007fd3d95f7a08>

It appears to be running the query as soon as the first scope is encountered. Is there something obvious I'm missing, or does this look like an issue with TS?

Hairworm answered 13/11, 2012 at 10:17 Comment(3)
User class has a db field called is_deleted? with boolean type?Bennettbenni
@Lichtamberg - yeah, the column is there and is boolean. Either scope works fine on it's own - it's only when they are chained that it breaks.Hairworm
could you show the response if you put Admin.by_account(1) into the console?Bennettbenni
M
0

Here are some problems people before you have had with sphinx_scopes that could be the problem here.

  • People have found problems with name conflicts in their sphinx_scopes. Your scope by_account is a prime candidate for that, so try renaming it. I can imagine situations where that gets messed up in a derived class but not the base class.
  • Scope problems in the past have been worked around by reordering the calls, so try Admin.without_deleted.by_account(1). Not a fix, I know.
  • I'd also predict that account_id is a primary key (i.e. at most one User per Account). If so, this could explain why Rails is choosing to fetch it prematurely.
Maramarabel answered 22/11, 2012 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.