[ts] undefined method `constantize' for nil:NilClass

398 views
Skip to first unread message

Oscar Del Ben

unread,
Apr 19, 2010, 11:45:20 AM4/19/10
to Thinking Sphinx
Hi, I'm having troubles doing a search on multiple models with TS. If
I use this code in one of my controllers:

ThinkingSphinx.search("*a*")

I get the undefined method `constantize' for nil:NilClass error.

The nice thing is that it works fine in the console (always in
development mode), but not in the controller. These are my indexes:

define_index do
set_property :delta => true
indexes login, :sortable => true
indexes province
indexes country
indexes state
where "state in ('active', 'suspended')"
end

define_index do
set_property :delta => true
indexes :updated_at, :sortable => true
indexes :body_html
end

--
You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
To post to this group, send email to thinkin...@googlegroups.com.
To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.

Oscar Del Ben

unread,
Apr 21, 2010, 5:02:33 AM4/21/10
to Thinking Sphinx
Actually, if I set config.cache_classes = true in development.rb,
everything works fine.

Pat Allan

unread,
Apr 23, 2010, 1:39:41 AM4/23/10
to thinkin...@googlegroups.com
Hi Oscar

If you're happy with that setting being true, then I guess there's no problems... still, things should work either way. What version of Thinking Sphinx are you using?

--
Pat

Oscar Del Ben

unread,
Apr 23, 2010, 3:37:07 AM4/23/10
to Thinking Sphinx
Thinking Sphinx v1.3.16
sphinx @0.9.8.1_0 (active)

Pat Allan

unread,
Apr 24, 2010, 12:30:00 AM4/24/10
to thinkin...@googlegroups.com
Hmm, still not sure what the cause is. What version of Rails are you using?

--
Pat

nruth

unread,
Oct 26, 2011, 6:08:53 PM10/26/11
to thinkin...@googlegroups.com
I have the same behaviour now. It was quite confusing.

    rails (3.1.0)
  thinking-sphinx (= 2.0.5)

Cheers,
Nick

Pat Allan

unread,
Oct 27, 2011, 1:55:16 AM10/27/11
to thinkin...@googlegroups.com
Hi Nick

Did restarting Rails get things working again? Or is the problem persisting?

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/0SeV-3BbC2kJ.

nruth

unread,
Oct 27, 2011, 7:35:59 PM10/27/11
to thinkin...@googlegroups.com
I've been disabling caching to get around the issue, but this is problematic because editing views and reloading the page becomes impossible (have to restart the webserver instead, much slower).

I think I've isolated the problem though. If I have my indices in class_eval blocks inside a rails initialiser (I want to index some models implemented in an engine -- specifically the radar/forem engine). When I comment out the eval blocks for ::Forem::Post and ::Forem::Topic, and move my own model's indices into the model file (where they normally go) then development mode etc works properly.

While this is fine for my own model files (personal preferences aside) I'm not sure how best to go about indexing the engine models.
Is this something you have any experience with?

It's not a disaster if not, it still works in production and test modes, just slows down styling the search results page.

Cheers,
Nick

Pat Allan

unread,
Oct 27, 2011, 11:27:05 PM10/27/11
to thinkin...@googlegroups.com
Hmm, I guess it's related to how TS detects indices. Can you shift the class_eval blocks into modules in a certain directory, and then add that directory path to ThinkingSphinx::Configuration.instance.model_directories in the initialiser instead?

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/ubzxcdyiKskJ.

nruth

unread,
Oct 29, 2011, 7:08:17 PM10/29/11
to thinkin...@googlegroups.com
That did the trick, thanks Pat!

Here's my layout:

app/indices/sphinx_indices.rb (could be more than 1 file, I am just being lazy and keeping what I had before)

  ::Forem::Post.class_eval do
    define_index do
      # fields
      indexes text, :sortable => false
      indexes user.username, :as => :author, :sortable => true

      # attributes
      has user_id, topic_id, created_at, updated_at
    end
  end
  … etc

config/initializers/thinking_sphinx.rb 

  ThinkingSphinx::Configuration.instance.model_directories << "app/indices"

and it now works in production with class caching, and development without class caching.

Cheers, 
Nick

Pat Allan

unread,
Nov 1, 2011, 4:21:20 PM11/1/11
to thinkin...@googlegroups.com
Great to hear Nick, and thanks for sharing the finer details of your solution :)

--
Pat

> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/bdbo5dgONXcJ.

Reply all
Reply to author
Forward
0 new messages