# Getting Started

{% hint style="warning" %}
This guide is relevant for **Ruby on Rails** with **ActiveRecord**. It assumes you have a **User** model using [Devise](https://github.com/plataformatec/devise) as the authentication framework.

If you want to see how doorkeeper integrates with an existing application, check out the [doorkeeper-provider-app](https://github.com/doorkeeper-gem/doorkeeper-provider-app/) repository, which is based on this guide.
{% endhint %}

## Installation

The first step is to add Doorkeeper to your project's dependencies:

```
bundle add doorkeeper
```

After that, you need to generate relevant files with:

```
bundle exec rails generate doorkeeper:install
```

This will introduce three changes:

1. A new **initializer** in `config/initializers/doorkeeper.rb`
2. Add doorkeeper's **routes** to `config/routes.rb`
3. **Locale** files in `config/locales/doorkeeper.en.yml`

## Migrations

To generate appropriate tables, run:

```
$ bundle exec rails generate doorkeeper:migration
    create  db/migrate/20190324080634_create_doorkeeper_tables.rb
```

This migration will create all necessary tables for [oAuth2 Applications](broken://pages/-L_cDSIWCBwco0S2p0T6), [Access Grants](broken://pages/-LainQOfP8ZKK_1nd8Bk), and [Access Tokens](broken://pages/-LainV77dRllU3VcvU7z). See [the database design](/guides/internals/database-design.md) for more details.

NOTE: If using UUIDs instead of integer IDs, see [Using PostgreSQL UUIDs as primary keys with Doorkeeper](https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-PostgreSQL-UUIDs-as-primary-keys-with-Doorkeeper) for changes you will need to make to your migration.

### Integrating with existing User Model

Before executing the migration, you may want to add foreign keys to doorkeeper's tables to ensure data integrity. Go to the migration file and uncomment the lines below:

{% code title="db/migrate/20190324080634\_create\_doorkeeper\_tables.rb" %}

```ruby
# Uncomment below to ensure a valid reference to the resource owner's table
add_foreign_key :oauth_access_grants, :users, column: :resource_owner_id
add_foreign_key :oauth_access_tokens, :users, column: :resource_owner_id
```

{% endcode %}

Now you're ready to run the migrations:

```
bundle exec rake db:migrate
```

As the next step, you may want to add associations to your model. If you skip this step, you'll encounter `ActiveRecord::InvalidForeignKey`error when you try to destroy the `User` that has associated access grants or access tokens.

{% code title="app/models/user.rb" %}

```ruby
class User < ApplicationRecord
  has_many :access_grants,
           class_name: 'Doorkeeper::AccessGrant',
           foreign_key: :resource_owner_id,
           dependent: :delete_all # or :destroy if you need callbacks

  has_many :access_tokens,
           class_name: 'Doorkeeper::AccessToken',
           foreign_key: :resource_owner_id,
           dependent: :delete_all # or :destroy if you need callbacks
end
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doorkeeper.gitbook.io/guides/ruby-on-rails/getting-started.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
