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

Cop idea: Prefer pluck(:column) over select(:column).map(&:column) #1075

Closed
Earlopain opened this issue Aug 22, 2023 · 0 comments · Fixed by #1102
Closed

Cop idea: Prefer pluck(:column) over select(:column).map(&:column) #1075

Earlopain opened this issue Aug 22, 2023 · 0 comments · Fixed by #1102
Labels
feature request Request for new functionality

Comments

@Earlopain
Copy link
Contributor

I have some code like this:

Tag.name_matches(query).select("name").limit(Config.tag_query_limit).map(&:name)

It would be nice if Rubocop could suggest the following code:

Tag.name_matches(query).limit(Config.tag_query_limit).pluck(:name)

There also should be some performance improvement since it skips instantiating the model class for matches.

@koic koic added the enhancement New feature or request label Aug 22, 2023
@koic koic added feature request Request for new functionality and removed enhancement New feature or request labels Sep 1, 2023
koic added a commit to koic/rubocop-rails that referenced this issue Sep 2, 2023
Resolves rubocop#1075.

This PR adds new `Rails/SelectMap` cop that checks for uses of `select(:column_name)` with `map(&:column_name)`.
These can be replaced with `pluck(:column_name)`.
There also should be some performance improvement since it skips instantiating the model class for matches.

```ruby
# bad
Model.select(:column_name).map(&:column_name)

# good
Model.pluck(:column_name)
```
koic added a commit to koic/rubocop-rails that referenced this issue Sep 4, 2023
Resolves rubocop#1075.

This PR adds new `Rails/SelectMap` cop that checks for uses of `select(:column_name)` with `map(&:column_name)`.
These can be replaced with `pluck(:column_name)`.
There also should be some performance improvement since it skips instantiating the model class for matches.

```ruby
# bad
Model.select(:column_name).map(&:column_name)

# good
Model.pluck(:column_name)
```
@koic koic closed this as completed in #1102 Sep 7, 2023
koic added a commit that referenced this issue Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request for new functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants