. . .

Enum column in Rails

Published: April 30, 2014

On This Page

    Declare an enum attribute where the values map to integers in the database, but can be queried by name. Example:

    class User < ActiveRecord::Base
      enum status: [ :admin, :user, :banned ]
    end

     For migration, write 

      def up
        create_table :authorities do |t|
          t.string 'value', :default => '', :null => false
          t.integer :status
        end
      end
    

     

    Don't forget to share this post

      Let's Build Digital Excellence Together


      • Cost Efficient Solutions.
      • Minimal Timelines.
      • Effective Communication.
      • High Quality Standards.
      • Lifetime Support.
      • Transparent Execution.
      • 24/7 Availability.
      • Scalable Teams.

      Join Our 200+ Happy Clients Across Globe


      Free Consultation.

        Do you need tech help of your startup/business? Experts from our team will get in touch with you.

        Please do not post jobs/internships inquiries here.