Shelvacu
Shelvacu

Reputation: 4380

Rails how to find where enum is defined?

I recently got the error:

You tried to define an enum named "label_type" on the model "Spree::ShippingMethod", but this will generate a instance method "label_type=", which is already defined by another enum.

The backtrace gives me the location of the enum that I attempted to define second, but not the first enum that was defined. Grepping through my codebase only brings up the file in the backtrace, so I suspect it's in a gem, but I don't how to find which gem it's in.

To be clear, I'm aware that the error I gave means that the enum has already been defined somewhere else. In the course of fixing this problem, I'm trying to figure out where it was defined.

Upvotes: 0

Views: 283

Answers (2)

bogl
bogl

Reputation: 1864

It might be defined in a gem. It could be defined through meta-programming. Maybe you can find out more with the help of Pry:

  • add 'pry' to your Gemfile (and run bundle install)
  • place "binding.pry" in the ruby code, just before the location where the error occurs
  • run the application. It will stop at the binding.pry command.
  • At the pry prompt, type show-method label_type=
  • type exit-p or !!! to leave pry when done.

Upvotes: 1

Mohanraj
Mohanraj

Reputation: 4220

I think you already have column or instance method label_type in that model. Basically when you define enum on a column it will create read/write method for setting that column value. FYI: Please refer this page http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

Upvotes: 0

Related Questions