2
class Klass < ApplicationRecord

  enum dimension: { length: 'length', breadth: 'breadth' }
end

I'm getting the following error :-

ArgumentError: You tried to define an enum named "name" on the model "Klass", but this will generate a class method "length", which is already defined by ActiveRecord::Relation.

I just want to know what are my options to get dimension enum working with length as one of its value. Is it possible to create the enum without defining length method on the model ?

3
  • Why would you want to do this in the first place? If you models just have two dimensions tops why do you want to add another layer of joins and complexity? This stinks of the EAV anti-pattern. Commented Feb 11, 2020 at 12:06
  • @max I have simplified my code and renamed the values to some generic ones. The original one has many values, with one value being length. Commented Feb 11, 2020 at 12:09
  • 1
    Why not just add a two columns to the table that actually has the attribute so that you don't have to create ridiculous Thing.joins(:attributes).where(dimension: 'length', value: 15) queries? Commented Feb 11, 2020 at 12:31

1 Answer 1

4

length is a reserved method name. So, you can't use this reserved method name.

you can try like using the prefix :

class Klass < ApplicationRecord
  enum dimension: { length: 'length', breadth: 'breadth' }, _prefix: :dimension
end

Note: Class method "length", which is already defined by ActiveRecord::Relation.

Sign up to request clarification or add additional context in comments.

2 Comments

length is not a keyword. Its a method name. Keywords are words with special significance in the language like if, else, class, module.
I changed as per suggestion(@max)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.