-
Notifications
You must be signed in to change notification settings - Fork 21.7k
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
Add foreign_key_exists?
method.
#18662
Conversation
@estum thank you for your work. Would you mind sharing some use-cases where you can see this being useful? |
@senny I was missing a method like |
# foreign_key_exists?(:accounts, name: "special_fk_name") | ||
# | ||
def foreign_key_exists?(from_table, options_or_to_table = {}) | ||
return unless supports_foreign_keys? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably return false instead of nil here to maintain convention with most Ruby .exist?
functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ianks that's not necessary. Rails does not make the promise of returning real booleans from predicate methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason for now? I would consider it an anti-pattern to return nil from a predicate method as it makes it harder to reason about the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ianks there is a section on booleans in our API documentation guidelines.
Add `foreign_key_exists?` method.
@estum great work! 💛 I added a CHANGELOG entry and made some minor changes in the merge commit (removed trailing whitespace, replaced The extraction of |
@@ -650,6 +658,10 @@ def remove_references(*args) | |||
def foreign_key(*args) # :nodoc: | |||
@base.add_foreign_key(name, *args) | |||
end | |||
|
|||
def foreign_key_exists?(*args) # :nodoc: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@senny Is this # :nodoc:
intentional? Or should we make this method publicly documented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claudiob I think it was due to the fact that foreign_key
had a no-doc as well. It was previously only used by references
. Should be fine to document them though.
No description provided.