Skip to content
Janko Marohnić edited this page Nov 6, 2021 · 15 revisions

When using multiple Rodauth configurations, you'll likely want to save the information of which account belongs to which configuration to the database. One way would be to have a separate table that stores account types:

$ rails generate migration create_account_types
# db/migrate/*_create_account_types.rb
class CreateAccountTypes < ActiveRecord::Migration
  def change
    create_table :account_types do |t|
      t.references :account, foreign_key: { on_delete: :cascade }, null: false
      t.string :type, null: false
    end
  end
end
$ rails db:migrate

Then an entry would be inserted after account creation, and optionally whenever Rodauth retrieves accounts you could filter only those belonging to the current configuration:

# app/lib/rodauth_app.rb
class RodauthApp < Rodauth::Rails::App
  configure(:admin) do
    # ...
    after_create_account do
      db[:account_types].insert(account_id: account_id, type: "admin")
    end
    auth_class_eval do
      def account_ds(*)
        super.join(:account_types, account_id: :id).where(type: "admin")
      end
    end
    # ...
  end
end
Clone this wiki locally