rails: Can't add enum field: method already defined by Active Record

➜  crm git:(master) ✗ rails console
/Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:179:in `detect_enum_conflict!': You tried to define an enum named "action" on the model "Relationship", but this will generate a instance method "update!", which is already defined by Active Record. (ArgumentError)
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:125:in `block (3 levels) in enum'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:117:in `each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:117:in `each_with_index'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:117:in `each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:117:in `block (2 levels) in enum'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:92:in `module_eval'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:92:in `block in enum'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:83:in `each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activerecord-4.2.5/lib/active_record/enum.rb:83:in `enum'
    from /Users/heaven/workspace/crm/app/models/relationship.rb:4:in `<class:Relationship>'
    from /Users/heaven/workspace/crm/app/models/relationship.rb:3:in `<top (required)>'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `load'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `block in load_file'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:647:in `new_constants_in'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:456:in `load_file'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:354:in `require_or_load'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:184:in `const_missing'
    from /Users/heaven/workspace/crm/app/models/relationship_ar_observer.rb:23:in `<class:RelationshipArObserver>'
    from /Users/heaven/workspace/crm/app/models/relationship_ar_observer.rb:3:in `<top (required)>'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `load'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:457:in `block in load_file'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:647:in `new_constants_in'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:456:in `load_file'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:354:in `require_or_load'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:184:in `const_missing'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:183:in `instantiate_observer'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:108:in `block in instantiate_observers'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:108:in `each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/rails-observers-0.1.2/lib/rails/observers/active_model/observing.rb:108:in `instantiate_observers'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/rails-observers-0.1.2/lib/rails/observers/railtie.rb:24:in `block (2 levels) in <class:Railtie>'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:27:in `each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/rails-observers-0.1.2/lib/rails/observers/railtie.rb:23:in `block in <class:Railtie>'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:36:in `call'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:44:in `each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/application/finisher.rb:62:in `block in <module:Finisher>'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `call'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
    from /Users/heaven/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
    from /Users/heaven/workspace/crm/config/environment.rb:5:in `<top (required)>'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/application.rb:328:in `require_environment!'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:142:in `require_application_and_environment!'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:67:in `console'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/heaven/.rvm/gems/ruby-2.2.1@crm/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:10:in `require'
    from bin/rails:10:in `<main>'

I do use Rails for many other projects with no issues, but here I do use Observers and have to preload my models.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

FWIW, I hit this error and understood there was a collision issue, but it took me a while to realize it was one of the enum values, not the enum name itself that was the causing the issue.

Obvious in hindsight, wasn’t obvious right away. Also, after realizing, I remembered I have hit this issue in the past, and stumbled through the same realization then too!

+1 to changing the error message.

@heaven I believe the confusion is why the error showed you problem with ‘update’ enum :action => %i[create update] will try to define update, update? and update! methods. Ref: https://github.com/vipulnsward/rails/blob/b7cce1c0a92ef88341137290eefd19ef688c175b/activerecord/lib/active_record/enum.rb#L7-L26

These are already defined by AR and hence the error