Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When used with sunspot gem: method_missing_without_enumerated_attribute already exists. #60

Open
aisrael opened this issue Nov 23, 2012 · 2 comments

Comments

@aisrael
Copy link

aisrael commented Nov 23, 2012

Scenario:

Use enumerated_attribute on some models, then try to use sunspot gem.

When you declare a model to be searchable using sunspot, then attempt to do rake sunspot:solr:reindex to build your index, you get:

rake aborted!
method_missing_without_enumerated_attribute already exists.  Circular references not permitted.
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/meta_programming-0.2.2/lib/meta_programming/object.rb:43:in `block in safe_alias_method_chain'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/meta_programming-0.2.2/lib/meta_programming/object.rb:39:in `class_eval'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/meta_programming-0.2.2/lib/meta_programming/object.rb:39:in `safe_alias_method_chain'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/meta_programming-0.2.2/lib/meta_programming/object.rb:66:in `define_chained_method'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/bundler/gems/enumerated_attribute-84221b872f7f/lib/enumerated_attribute/integrations/active_record.rb:107:in `singletonclass'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/bundler/gems/enumerated_attribute-84221b872f7f/lib/enumerated_attribute/integrations/active_record.rb:94:in `block in define_enumerated_attribute_new_method'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/bundler/gems/enumerated_attribute-84221b872f7f/lib/enumerated_attribute/integrations/active_record.rb:93:in `class_eval'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/bundler/gems/enumerated_attribute-84221b872f7f/lib/enumerated_attribute/integrations/active_record.rb:93:in `define_enumerated_attribute_new_method'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/bundler/gems/enumerated_attribute-84221b872f7f/lib/enumerated_attribute/attribute.rb:45:in `create_enumerated_attribute'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/bundler/gems/enumerated_attribute-84221b872f7f/lib/enumerated_attribute.rb:13:in `enumerated_attribute'
/Users/aisrael/workspaces/rails/likejobs/web/app/models/user.rb:22:in `<class:User>'
/Users/aisrael/workspaces/rails/likejobs/web/app/models/user.rb:1:in `<top (required)>'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/sunspot_rails-1.3.3/lib/sunspot/rails/tasks.rb:34:in `block (3 levels) in <top (required)>'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/sunspot_rails-1.3.3/lib/sunspot/rails/tasks.rb:34:in `each'
/Users/aisrael/.rvm/gems/ruby-1.9.3-p327/gems/sunspot_rails-1.3.3/lib/sunspot/rails/tasks.rb:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => sunspot:solr:reindex => sunspot:reindex
(See full trace by running task with --trace)

This happens even if the searchable model itself doesn't declare any enumerated_attributes.

@aisrael
Copy link
Author

aisrael commented Nov 23, 2012

I just discovered that you don't even need sunspot gem. If you have

class User < ActiveRecord::Base
    enum_attr :test, %w(a b c)

Then it's enough to go

$ rails console
> class User
>   enum_attr :foo, %w(d e f)

And you'll get the same error. From what I can tell, in enumerated_attribute/integrations/active_record.rb the following guard:

unless private_method_defined?(:method_missing_without_enumerated_attribute)

is failing.

@aisrael
Copy link
Author

aisrael commented Nov 23, 2012

Found it. The check in meta_programming/lib/meta_programming/object.rb:42 checks for both public and private methods:

if (method_defined?(method_name_with_ext) || private_method_defined?(method_name_with_ext))
    raise(MetaProgramming::AliasMethodChainError, "#{method_name_without_ext} already exists.  Circular references not permitted.")

Whereas the guard in enumerated_attribute/integrations/active_record.rb:106 only checks for private methods. It should be changed to:

unless method_defined?(:method_missing_without_enumerated_attribute) || private_method_defined?(:method_missing_without_enumerated_attribute)

Forking, patching, submitting a pull request.

cattywampus added a commit to cattywampus/enumerated_attribute that referenced this issue Oct 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant