How to use Postgres Enums with Rails 7
#codesummary #code #summary #rails #rails7 #postgresql #enums
This is a code summary for https://allaboutcoding.ghinda.com/how-to-work-with-postgresql-enums-in-rails-7
![# Part 1/3: How to create an enum type for an Active Record Model def up create_enum :user_status, ["pending", "active", "archived"] # creating enum type create_table :users, force: true do |t| t.enum :status, enum_type: "user_status", default: "pending", null: false end end def down drop_table :users execute <<-SQL DROP TYPE user_status; SQL end # This is how the model might look like when using standard Rails class User < ActiveRecord::Base enum status: { pending: 'pending', active: 'active', }, _prefix: true end # the following queries will work User.status_pending.count user.status_pending?](https://archive-assets.shortruby.com/assets/issues/328369cc4737e397cb439fc1b15963fa409b1d989f6073447c8af21cad225d36.png)


If you want to play with the code, I create a single file Rails app here: postgres_enums_in_rails_7.rb