Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation for adding foreign keys #1025

Merged
merged 3 commits into from
Feb 2, 2018

Conversation

fschwahn
Copy link
Contributor

@fschwahn fschwahn commented Jan 29, 2018

Add explanation about necessity to define relations when adding foreign keys as mentioned in #1007

Add explanation about necessity to define relations when adding foreign keys.
README.md Outdated
@@ -86,6 +86,17 @@ for each table that includes a `resource_owner_id` column:
add_foreign_key :table_name, :users, column: :resource_owner_id
```

Remember to add associations to your model so the related records are deleted.
If you don't do this a `ActiveRecord::InvalidForeignKey`-error will be raised
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's write "an ActiveRecord::InvalidForeignKey", ok?

README.md Outdated
@@ -86,6 +86,17 @@ for each table that includes a `resource_owner_id` column:
add_foreign_key :table_name, :users, column: :resource_owner_id
```

Remember to add associations to your model so the related records are deleted.
If you don't do this a `ActiveRecord::InvalidForeignKey`-error will be raised
when you try to delete a model with related access grants or access tokens.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete method will ignore callbacks for associations, so let's write "try to destroy the model" instead to avoid misunderstanding.

README.md Outdated

```ruby
class User < ApplicationRecord
has_many :oauth_access_grants, class_name: "Doorkeeper::AccessGrant", foreign_key: :resource_owner_id, dependent: :destroy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really required to name associations like oauth_access_grants ? Doorkeeper::AccessGrant already has table name prefixes, so Rails must recognize the name of the table for associations.

Maybe it will be better to name them like:

has_many :access_grants, ...
has_many :access_tokens, ...

Also I suggest to change dependent: :destroy option to dependent: :delete_all - it will delete all the associated records in one SQL query instead of N+1. But we can save :destroy variant in comment, like " # or :destroy if you need callbacks"

What do you think?

@nbulaj
Copy link
Member

nbulaj commented Jan 29, 2018

Hi @fschwahn . Can you please take a look at my comments above? Also please add a small changelog entry to NEWS.md and rebase commits to squash them into one. Thanks for your work!

@nbulaj
Copy link
Member

nbulaj commented Feb 2, 2018

Hi @fschwahn . Any updates here?

@nbulaj nbulaj added the docs label Feb 2, 2018
@fschwahn
Copy link
Contributor Author

fschwahn commented Feb 2, 2018

Had a busy week, thanks for reminding me. I incorporated all your suggestions, all of them make sense. I did all the editing using Github, I don't have the repository checked out, so I can't easily squash. But you can Squash & Merge using Github UI.

@nbulaj nbulaj merged commit d68543e into doorkeeper-gem:master Feb 2, 2018
@nbulaj
Copy link
Member

nbulaj commented Feb 2, 2018

Thank you @fschwahn, good work! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants