I am using in rails and getting following error:
undefined method `enum' for #<Class:0x007f03202a1190
Model
class Location < ActiveRecord::Base
enum status: [ :current, :preffered ]
end
How can i remove this error.
If the constants in enumType define a set of bit fields and value contains the values, names, or underlying values of multiple bit fields, the IsDefined method returns false. In other words, for enumerations that define a set of bit fields, the method determines only whether a single bit field belongs to the enumeration.
valueOf () method returns the enum constant of the specified string value, if exists. enum can contain a constructor and it is executed separately for each enum constant at the time of enum class loading. We can’t create enum objects explicitly and hence we can’t invoke enum constructor directly.
Enum declaration can be done outside a Class or inside a Class but not inside a Method. First line inside enum should be list of constants and then other things like methods, variables and constructor. According to Java naming conventions, it is recommended that we name constant with all capital letters
enum can contain a constructor and it is executed separately for each enum constant at the time of enum class loading. We can’t create enum objects explicitly and hence we can’t invoke enum constructor directly. enum can contain both concrete methods and abstract methods.
ActiveRecord::Enum
was only introduced to Rails at commit db41eb8a
, and so far this commit has only been released with Rails tag v4.1.0.beta1
.
It's likely that the current Rails gem you're using does not yet have this commit, and so does not have the code for implementing enum
.
To check to see which version of the Rails gem you have, run:
bundle show rails
I just ran bundle update
and then bundle show rails
, and I am showing:
[PATH TO YOUR GEMS]/rails-4.0.2
This version of the gem does not include the code with enum
. You can see this by comparing what is in v4.0.2
with what is in v4.0.1.beta1
. If you click on 'File Changed' and then do a search in the page for enum.rb
, you'll see that that is completely newly added code.
If you want to ensure that you get the newly tagged version of Rails, you can modify your Gemfile so that your line for including rails looks like this:
gem 'rails', :git => 'git://github.com/rails/rails.git', :tag => 'v4.1.0.beta1'
After you do a bundle update
, you can see by doing a bundle show rails
that you have the following rails gem:
[PATH TO YOUR GEMS]/rails-f706d5f945c5
f706d5f945c5
is the commit that was tagged for release v4.1.0.beta1
.
After you get this bleeding edge version of the Rails gem, you should have access to this enum
functionality.
You can use this feature by copying the code in the file rails/activerecord/lib/active_record/enum
plus these lines:
module ActiveRecord
class Base
extend ActiveRecord::Enum
end
end
to your app's lib/
directory and then require
-ing it in your environment.rb
file. Eg.: in config/environment.rb
require 'active_record_enum'
You may refer to this code we have in production.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With