#105 ✓invalid
humanzz

Ruby 1.9 - Anonymous modules have no name to be referenced by

Reported by humanzz | March 28th, 2010 @ 04:28 AM | in Bugs

Hi,
I've been using sunspot_rails 1.0.5 smoothly with a Rails 2.3.5 application on Ruby 1.8.7.
I decided to give it a try on Ruby 1.9.1.

I defined a method on a model to do internally call solr_search method. That method is called from a controller to provide the search functionality. Whenever I call that method from the controller, I get the exception (Anonymous modules have no name to be referenced by) thrown in my face.

I tried to call the model method directly from the ruby console with the exact set of params, it surprisingly works just fine.

Here's the method definition

class Candidate
  .
  .
  def self.employers_search(q, page = 1, per_page = 10)
    q = q.clone
    sorter = "job_start_date"
    sorter_dir = "asc"
    
    solr_search do
      fulltext(q[:keywords]) unless q[:keywords].blank?
      all_of do
        if q[:gender_id]
          any_of do
            # THE FOLLOWING IS THE LINE THAT THROWS THE EXCEPTION
            with(:gender_id, Gender[:male]) if q[:gender_id].include?(Gender[:male].to_s)
            all_of do
              with(:gender_id, Gender[:female])
              with(:job_mixing_id).any_of(q[:job_mixing_id])
            end if q[:gender_id].include?(Gender[:female].to_s)
          end
          q.delete :gender_id
          q.delete :job_mixing_id
        end
        q.each_pair do |k,v|
          if k =~ /_ids?$/
            non_empty_v = Array(v).reject {|x| x.blank? }
            with(k).any_of(non_empty_v) if non_empty_v.length > 0
          elsif k =~ /_gpas$/
            with(k).greater_than(v.is_a?(Array) ? v.first : v)
          end
        end
      end
      order_by sorter, sorter_dir
      paginate :page => page, :per_page => per_page
    end
  end
  .
  .
end

Here's the stack trace

ArgumentError (Anonymous modules have no name to be referenced by):
  app/models/candidate.rb:208:in `block (3 levels) in employers_search'
  sunspot (1.0.5) lib/sunspot/util.rb:224:in `instance_eval'
  sunspot (1.0.5) lib/sunspot/util.rb:224:in `instance_eval_with_context'
  sunspot (1.0.5) lib/sunspot/util.rb:86:in `instance_eval_or_call'
  sunspot (1.0.5) lib/sunspot/dsl/scope.rb:148:in `any_of'
  sunspot (1.0.5) lib/sunspot/util.rb:244:in `method_missing'
  app/models/candidate.rb:207:in `block (2 levels) in employers_search'
  sunspot (1.0.5) lib/sunspot/util.rb:224:in `instance_eval'
  sunspot (1.0.5) lib/sunspot/util.rb:224:in `instance_eval_with_context'
  sunspot (1.0.5) lib/sunspot/util.rb:86:in `instance_eval_or_call'
  sunspot (1.0.5) lib/sunspot/dsl/scope.rb:172:in `all_of'
  sunspot (1.0.5) lib/sunspot/util.rb:244:in `method_missing'
  app/models/candidate.rb:205:in `block in employers_search'
  sunspot (1.0.5) lib/sunspot/util.rb:224:in `instance_eval'
  sunspot (1.0.5) lib/sunspot/util.rb:224:in `instance_eval_with_context'
  sunspot (1.0.5) lib/sunspot/util.rb:86:in `instance_eval_or_call'
  sunspot (1.0.5) lib/sunspot/search.rb:200:in `build'
  sunspot (1.0.5) lib/sunspot/session.rb:45:in `new_search'
  sunspot (1.0.5) lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `new_search'
  sunspot (1.0.5) lib/sunspot.rb:249:in `new_search'
  C:/ruby191rc2/lib/ruby/gems/1.9.1/gems/sunspot_rails-1.0.5/lib/sunspot/rails/searchable.rb:131:in `solr_search'
  app/models/candidate.rb:203:in `employers_search'
  app/controllers/employers/candidates_controller.rb:14:in `block (2 levels) in index'
  app/controllers/employers/candidates_controller.rb:7:in `index'
  <internal:prelude>:8:in `synchronize'
  C:/ruby191rc2/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
  C:/ruby191rc2/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
  C:/ruby191rc2/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Comments and changes to this ticket

  • mat

    mat March 29th, 2010 @ 02:00 PM

    • Milestone set to Bugs

    I'll try to get this fixed in a 1.1.x patchlevel, but probably won't have time to look at it right away. In the meantime, I think you should be able to work around it by explicitly scoping to the root namespace for your constants, e.g.:

    with(:gender_id, ::Gender[:male]) if q[:gender_id].include?(::Gender[:male].to_s)
    

    Thanks for the report!

  • humanzz

    humanzz March 29th, 2010 @ 06:40 PM

    Thanks, Mat, for the workaround. You made my day :)

  • mat

    mat July 14th, 2010 @ 02:41 PM

    • State changed from “new” to “invalid”
    • Milestone order changed from “0” to “0”

    I can't reproduce this in a test case in ruby 1.9.1p378. Marking invalid, but feel free to submit a patch with a failing test case and I'll reopen.

  • Nick Zadrozny

    Nick Zadrozny September 17th, 2010 @ 12:52 PM

    Seeing this in 1.9.2-p0 when running the sunspot_rails specs against Rails 2.3.9.

    BUNDLE_GEMFILE=spec/rails2/Gemfile RAILS_ROOT=spec/rails2/ bundle exec spec spec/
    /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/activesupport-2.3.9/lib/active_support/dependencies.rb:611:in `to_constant_name': Anonymous modules have no name to be referenced by (ArgumentError)
    
  • Nick Zadrozny

    Nick Zadrozny September 17th, 2010 @ 01:25 PM

    Actually, looks like the error I am seeing is something different. And fixed now anyway. Disregard my last ;)

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Awesome Solr interaction for Ruby

People watching this ticket

Pages