Skip to content

Allow passing nil to rewhere method #48120

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

Merged
merged 1 commit into from
May 4, 2023
Merged

Conversation

a5-stable
Copy link
Contributor

@a5-stable a5-stable commented May 3, 2023

Motivation / Background

This Pull Request has been created because [REPLACE ME]

Detail

This change allows nil to be passed as a parameter to the rewhere method in ActiveRecord, instead of raising an error.

before

Post.where(id: 1).rewhere(nil).to_sql
#=> ArgumentError (Unsupported argument type:  (NilClass))

after

Post.where(id: 1).rewhere(nil)
#=> "SELECT `posts`.* FROM `posts`"

Additional information

Currently, reorder(nil) is allowed to remove the current ordering, while rewhere(nil) raises an error. This change modifies the implementation of rewhere to allow nil to be passed as a parameter, bringing it in line with reorder.

Post.order(:created_at).reorder(nil)
#=> "SELECT `posts`.* FROM `posts`"

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Copy link
Member

@byroot byroot left a comment

Choose a reason for hiding this comment

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

One minor issue, but otherwise looks good to me.

@@ -944,6 +944,8 @@ def where!(opts, *rest) # :nodoc:
# This is short-hand for <tt>unscope(where: conditions.keys).where(conditions)</tt>.
# Note that unlike reorder, we're only unscoping the named conditions -- not the entire where statement.
def rewhere(conditions)
return spawn.unscope(:where) if conditions.nil?
Copy link
Member

Choose a reason for hiding this comment

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

unscope calls spawn already.

Suggested change
return spawn.unscope(:where) if conditions.nil?
return unscope(:where) if conditions.nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@byroot
thank you for your review 🙇‍♀️
I fixed it.

@a5-stable a5-stable force-pushed the rewhere-with-nil branch from 82afe58 to 0924095 Compare May 4, 2023 11:32
@a5-stable
Copy link
Contributor Author

a5-stable commented May 4, 2023

Thank you for your review, I've integrated the proposed modifications.

return spawn.unscope(:where) if conditions.nil?

return unscope(:where) if conditions.nil?

@byroot byroot merged commit 018b2ae into rails:main May 4, 2023
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.

2 participants