Re: undefined method `association_class' for nil:NilClass

618 views
Skip to first unread message

complistic

unread,
Sep 4, 2012, 8:02:29 AM9/4/12
to thinkin...@googlegroups.com
Hi Ahmed,

What makes you think this is an thinking-sphinx error? I have seen similar errors when upgrading an app from 3.0 to 3.1 without thinking-sphinx.

I think it was to do with calling associations on empty ActiveRecord result sets.

What is the code that creates @person, and whats its current value?
The error makes me think the value of @person is nil or an Association object (not a Person).

If @person is indeed a Person, Whats the value of @person.current_friends?

I hope I've given you a few new ideas.

David Bennett



On Tuesday, 4 September 2012 01:20:14 UTC+9:30, Daly wrote:
Hello,

I'm running thinking-sphinx 2.0.10 in a Rails 3.2 app.

rvm --version
rvm 1.14.12 (stable) by Wayne E. Seguin <waynee...@gmail.com>, Michal Papis <mpa...@gmail.com> [https://rvm.io/]

ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.2]

This used to be a rails 2.3 app that I'm converting. It always breaks at the exact same location with:

    undefined method `association_class' for nil:NilClass

The offending line is:
    @friends = @person.current_friends.order('RAND()').limit(5)

The association is:

In person.rb:
    has_many :current_friends, :source => :friend, :through => :friends

and the sphinx method in person.rb is:
    define_index do
      indexes :user_name
      indexes :id, :as => :id
      indexes :email, :as => :email
      indexes :account_verified#, :as => :account_verified

      set_property :delta => false
    end


In friend.rb:
    belongs_to :person
    belongs_to :friend, :class_name => 'Person', :foreign_key => "friend_id"
    validates_presence_of :person_id, :friend_id

The closes thing I could find that matches my issue is: https://github.com/pat/thinking-sphinx/issues/310, I did put the define index after all the declarations. I'm sure it's ts because when I comment out all define_index methods in the code, it works fine.

This has been hounding me for a long time now.

Thanks for your help,
Ahmed

Daly

unread,
Sep 4, 2012, 1:19:30 PM9/4/12
to thinkin...@googlegroups.com
The reason is that when I comment out the define_index declarations in the models, that part of the code runs fine. Putting it back in, breaks it. That lead me to believe it's a thinking-sphinx issue

Pat Allan

unread,
Sep 5, 2012, 7:32:06 AM9/5/12
to thinkin...@googlegroups.com
Hi Daly

Can you provide the full stack trace of the error? It might provide some clues as to how Thinking Sphinx is getting in the way.

--
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/-/rCxKBnWrdrAJ.
> 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.



Ahmed El-Daly

unread,
Sep 5, 2012, 8:37:14 AM9/5/12
to thinkin...@googlegroups.com
Thanks Pat,

Here it is:

/gems/activerecord-3.2.6/lib/active_record/associations.rb:157:in `association'
/gems/activerecord-3.2.6/lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
app/controllers/account_controller.rb:58:in `index'
/gems/actionpack-3.2.6/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:167:in `process_action'
/gems/actionpack-3.2.6/lib/action_controller/metal/rendering.rb:10:in `process_action'
/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:513:in `_run__1786129847638205132__process_action__213430523744088085__callbacks'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
/gems/actionpack-3.2.6/lib/abstract_controller/callbacks.rb:17:in `process_action'
/gems/actionpack-3.2.6/lib/action_controller/metal/rescue.rb:29:in `process_action'
/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `block in instrument'
/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/gems/activesupport-3.2.6/lib/active_support/notifications.rb:123:in `instrument'
/gems/actionpack-3.2.6/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
/gems/actionpack-3.2.6/lib/action_controller/metal/params_wrapper.rb:206:in `process_action'
/gems/activerecord-3.2.6/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/gems/newrelic_rpm-3.4.1/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:34:in `block in process_action'
/gems/newrelic_rpm-3.4.1/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:262:in `block in perform_action_with_newrelic_trace'
/gems/newrelic_rpm-3.4.1/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
/gems/newrelic_rpm-3.4.1/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:257:in `perform_action_with_newrelic_trace'
/gems/newrelic_rpm-3.4.1/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:33:in `process_action'
/gems/actionpack-3.2.6/lib/abstract_controller/base.rb:121:in `process'
/gems/actionpack-3.2.6/lib/abstract_controller/rendering.rb:45:in `process'
/gems/actionpack-3.2.6/lib/action_controller/metal.rb:203:in `dispatch'
/gems/actionpack-3.2.6/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
/gems/actionpack-3.2.6/lib/action_controller/metal.rb:246:in `block in action'
/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:73:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:36:in `call'
/gems/journey-1.0.4/lib/journey/router.rb:68:in `block in call'
/gems/journey-1.0.4/lib/journey/router.rb:56:in `each'
/gems/journey-1.0.4/lib/journey/router.rb:56:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:600:in `call'
/gems/oink-0.9.3/lib/oink/middleware.rb:17:in `call'
/gems/newrelic_rpm-3.4.1/lib/new_relic/rack/browser_monitoring.rb:12:in `call'
/gems/newrelic_rpm-3.4.1/lib/new_relic/rack/developer_mode.rb:24:in `call'
/gems/mongoid-3.0.5/lib/rack/mongoid/middleware/identity_map.rb:33:in `block in call'
/gems/mongoid-3.0.5/lib/mongoid/unit_of_work.rb:39:in `unit_of_work'
/gems/mongoid-3.0.5/lib/rack/mongoid/middleware/identity_map.rb:33:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
/gems/rack-1.4.1/lib/rack/etag.rb:23:in `call'
/gems/rack-1.4.1/lib/rack/conditionalget.rb:25:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/head.rb:14:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/flash.rb:242:in `call'
/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/cookies.rb:338:in `call'
/gems/activerecord-3.2.6/lib/active_record/query_cache.rb:64:in `call'
/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `_run__3765126766932519170__call__2902207070006020771__callbacks'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:405:in `__run_callback'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
/gems/activesupport-3.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/reloader.rb:65:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
/gems/railties-3.2.6/lib/rails/rack/logger.rb:26:in `call_app'
/gems/railties-3.2.6/lib/rails/rack/logger.rb:16:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/request_id.rb:22:in `call'
/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/gems/rack-1.4.1/lib/rack/runtime.rb:17:in `call'
/gems/activesupport-3.2.6/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
/gems/rack-1.4.1/lib/rack/lock.rb:15:in `call'
/gems/actionpack-3.2.6/lib/action_dispatch/middleware/static.rb:62:in `call'
/gems/airbrake-3.1.2/lib/airbrake/rack.rb:42:in `call'
/gems/airbrake-3.1.2/lib/airbrake/user_informer.rb:12:in `call'
/gems/railties-3.2.6/lib/rails/engine.rb:479:in `call'
/gems/railties-3.2.6/lib/rails/application.rb:220:in `call'
/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/gems/railties-3.2.6/lib/rails/rack/log_tailer.rb:17:in `call'
/gems/thin-1.4.1/lib/thin/connection.rb:80:in `block in pre_process'
/gems/thin-1.4.1/lib/thin/connection.rb:78:in `catch'
/gems/thin-1.4.1/lib/thin/connection.rb:78:in `pre_process'
/gems/thin-1.4.1/lib/thin/connection.rb:53:in `process'
/gems/thin-1.4.1/lib/thin/connection.rb:38:in `receive_data'
/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
/gems/thin-1.4.1/lib/thin/backends/base.rb:63:in `start'
/gems/thin-1.4.1/lib/thin/server.rb:159:in `start'
/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
/gems/railties-3.2.6/lib/rails/commands/server.rb:70:in `start'
/gems/railties-3.2.6/lib/rails/commands.rb:55:in `block in '
/gems/railties-3.2.6/lib/rails/commands.rb:50:in `tap'
/gems/railties-3.2.6/lib/rails/commands.rb:50:in `'
script/rails:6:in `require'
script/rails:6:in `'

I know it doesn't mention thinking-sphinx anywhere, but as I said below, commenting out define_index and gem 'thinking-sphinx' in the gem enables the app to hum along just fine.

Cheers,
Ahmed

Pat Allan

unread,
Sep 6, 2012, 5:51:56 PM9/6/12
to thinkin...@googlegroups.com
Something's playing havoc with the associations, but I'm a little confused as to why it'd be Thinking Sphinx, given you're not even using associations within your index definition. Do you have any other index definitions in other models?

Also, if it's possible, I'd be keen to reproduce this locally. Is getting access to your project's source an option? Or would you be able to create a sample app that also has this issue?

--
Pat

Ahmed El-Daly

unread,
Sep 6, 2012, 11:06:43 PM9/6/12
to thinkin...@googlegroups.com
I don't know what to say Pat. I've mutilated the gemfile to the absolute bare minimum, and it's still happening:

source 'http://gemcutter.org'
source 'http://gems.github.com'

gem 'rails', '3.2.6'

gem 'mysql2'
# gem 'delayed_job_active_record'
# gem 'geokit'
# gem 'geokit-rails3'
# gem 'email_veracity'
# gem 'uuid'
# gem 'acts_as_reportable'
# gem 'acts_as_list'
gem 'authlogic'
# gem 'airbrake'
# gem 'cmess'
# gem 'hpricot'
# gem 'simple-rss'
gem 'haml'
# gem 'acts_as_tree'
# gem 'oink'
# gem 'attachment_fu', :git => 'https://github.com/aeldaly/attachment_fu.git', :branch => 'master'
# gem 'aws-s3'
# gem 'will_paginate', '~> 3.0'
# gem 'mongoid', '~> 3.0.0'
# gem 'activerecord-import'
# gem 'money'
# gem 'make_resourceful'
# gem 'daemons-rails'
gem 'thinking-sphinx', '2.0.10'
# gem 'rest-client'
# gem 'twitter'
# gem 'jquery-rails', '>= 0.2.6'
# gem 'newrelic_rpm'
# gem 'ey_config'

# rails components moved to gems
# gem 'prototype-rails'
# gem 'dynamic_form' # provides error_messages and the like
# gem 'auto_complete', :git => 'https://github.com/Sharagoz/auto_complete.git'

# http://crimpycode.brennonbortz.com/?p=42
# gem 'escape_utils'

group :development do
# gem 'thin'
# gem 'rails-footnotes', '>= 3.7.5.rc4'
end

group :test do
#gem 'fixture_replacement'
end

group :development, :test do
# gem 'ZenTest'
# gem 'autotest-rails'
# gem 'rspec', '2.11.0'
# gem 'rspec-rails', '2.11.0'
# gem 'factory_girl'
# gem 'factory_girl_rails'
# gem 'forgery'
# gem 'database_cleaner'
#gem 'libnotify'
end

group :assets do
gem 'uglifier'
end

Look, there's almost nothing left. I can't unfortunately grant you access to the project. Would you be open to a Skype chat and I can share my screen with you?

This project is supposed to go live before the end of the month and this is the only thing that's in my way.

Thanks,
Ahmed

Ahmed El-Daly

unread,
Sep 6, 2012, 11:22:06 PM9/6/12
to thinkin...@googlegroups.com
I've isolated it to 1 file. If you remember, the line that causes the error is:

> @friends = @person.current_friends.order('RAND()').limit(5)

but, I found that if I uncomment define_index in all files but 1, it works fine. The file is review.rb

Here are the associations:

belongs_to :person, :counter_cache => true
belongs_to :wine

in person.rb
has_many :reviews, :dependent => :destroy
has_many :wines, :through => :reviews

Not sure if it makes any difference, but wine.rb also has a define_index block.

wine.rb:
has_many :reviews, :dependent => :destroy
has_many :user_reviews, :class_name => 'Review', :include => [:person]

Daly

unread,
Sep 14, 2012, 9:56:54 AM9/14/12
to thinkin...@googlegroups.com
Any pointers?
>>>>> To post to this group, send email to thinking-sphinx@googlegroups.com.
>>>>> To unsubscribe from this group, send email to thinking-sphinx+unsubscribe@googlegroups.com.
>>>>> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
>>>>
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
>>>> To post to this group, send email to thinking-sphinx@googlegroups.com.
>>>> To unsubscribe from this group, send email to thinking-sphinx+unsubscribe@googlegroups.com.
>>>> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
>>>>
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
>>> To post to this group, send email to thinking-sphinx@googlegroups.com.
>>> To unsubscribe from this group, send email to thinking-sphinx+unsubscribe@googlegroups.com.
>>> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
>> To post to this group, send email to thinking-sphinx@googlegroups.com.
>> To unsubscribe from this group, send email to thinking-sphinx+unsubscribe@googlegroups.com.

Pat Allan

unread,
Sep 14, 2012, 10:39:02 AM9/14/12
to thinkin...@googlegroups.com
Hi Ahmed

To be honest, I'm not sure I can get much further without some code in front of me. If you can create a basic Rails application that reproduces the issue and send that to me, that'd be fantastic.

Most people aren't seeing this issue, so getting it fixed is no simple matter.

That said, what's the define_index block in your review model?

--
Pat

> >>>>> 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.
> >>>>
> >>>>
> >>>>
> >>>> --
> >>>> 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.
> >>>>
> >>>
> >>>
> >>> --
> >>> 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.
> >>
> >>
> >>
> >> --
> >> 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.
> >>
> >
>
>
> --
> 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/-/jysKxBztSEIJ.
> To post to this group, send email to thinkin...@googlegroups.com.
> To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.

Pat Allan

unread,
Dec 7, 2012, 5:35:24 AM12/7/12
to thinkin...@googlegroups.com
That's super helpful Henrik - thanks for figuring it out! I'm guessing it won't happen with TS 3 though, given there the index definitions aren't in the models.

On 05/12/2012, at 12:38 AM, Henrik Nyh wrote:

> We're looking into this issue as well. We haven't gotten to the bottom of it yet, but we've noticed some possibly interesting facts.
>
> We have reduced the issue to something like
>
> class Item < ActiveRecord::Base
> define_index do
> end
> has_one :my_association
> end
>
> class SubItem < Item
> end
>
> When running some tests with spork (but not without) that create an Item, we will get

>
> undefined method `association_class' for nil:NilClass
>

> when the factory tries to use `my_association`.
>
> We can unbreak it by duplicating "has_one :my_association" in the SubItem class.
>
> We can also unbreak it by moving the association in the Item class above "define_index do; end".
>
> Our current theory is that we hit "define_index", Thinking Sphinx does something with associations to be able to do its "index foo.bar" association magic, and that causes Item not to define the association when it should.
>
> Just sharing this to give Pat (and possibly others) something to go on if we don't come any further, and before I forget what we've found :)

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

Reply all
Reply to author
Forward
0 new messages