rails g model AdminSettings --force-plural
class CreateAdminSettings < ActiveRecord::Migration[7.0]
def change
create_table :admin_settings do |t|
t.boolean :singleton_guard, default: true, null: false
t.timestamps
end
add_index :admin_settings, :singleton_guard, unique: true
end
end
class AdminSettings < ApplicationRecord
validates :singleton_guard, inclusion: { in: [true] }, uniqueness: true
def self.instance
first_or_create(singleton_guard: true)
end
end
Now you can call AdminSettings.instance
to fetch the object or create one.