Skip to content

Add foreign_key_exists? method.#18662

Merged
senny merged 2 commits intorails:masterfrom
estum:foreign-key-exists
Feb 16, 2015
Merged

Add foreign_key_exists? method.#18662
senny merged 2 commits intorails:masterfrom
estum:foreign-key-exists

Conversation

@estum
Copy link
Copy Markdown
Contributor

@estum estum commented Jan 23, 2015

No description provided.

@senny
Copy link
Copy Markdown
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
Copy Markdown
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.

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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).

Copy link
Copy Markdown
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
Copy Markdown
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.

5 participants