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

Add foreign_key_exists? method. #18662

Merged
merged 2 commits into from Feb 16, 2015
Merged

Add foreign_key_exists? method. #18662

merged 2 commits into from Feb 16, 2015

Conversation

estum
Copy link
Contributor

@estum estum commented Jan 23, 2015

No description provided.

@senny
Copy link
Member

senny commented Jan 24, 2015

@estum thank you for your work. Would you mind sharing some use-cases where you can see this being useful?

@senny senny added this to the 5.0.0 milestone Jan 24, 2015
@estum
Copy link
Contributor Author

estum commented Jan 24, 2015

@senny I was missing a method like index_exists? (which useful for me too) while reorganizing migrations to avoid add_foreign_key``remove_foreign_key methods raise when a key is already created\removed. I was surprised it yet hasn't been implemented.

# foreign_key_exists?(:accounts, name: "special_fk_name")
#
def foreign_key_exists?(from_table, options_or_to_table = {})
return unless supports_foreign_keys?
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

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.

@senny senny merged commit f0ae503 into rails:master Feb 16, 2015
senny added a commit that referenced this pull request Feb 16, 2015
Add `foreign_key_exists?` method.
@senny
Copy link
Member

senny commented Feb 16, 2015

@estum great work! 💛

I added a CHANGELOG entry and made some minor changes in the merge commit (removed trailing whitespace, replaced all?{|assoc|} with all? {|key, value|} and got rid of try(:name).

The extraction of foreign_key_for already paid off. Merging this I found a bug in a recent patch I committed (8980da8).

@@ -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:
Copy link
Member

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?

Copy link
Member

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.

@rafaelfranca rafaelfranca modified the milestones: 5.0.0 [temp], 5.0.0 Dec 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants