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

Introduce delete_by and destroy_by methods to ActiveRecord::Relation #35316

Merged
merged 1 commit into from Feb 19, 2019

Conversation

Projects
None yet
5 participants
@abhaynikam
Copy link
Contributor

abhaynikam commented Feb 18, 2019

closes #35304

Adds delete_by and destroy_by as relation methods to add missing symmetry.

@r? @dhh

@rails-bot rails-bot bot added the activerecord label Feb 18, 2019

@abhaynikam abhaynikam force-pushed the abhaynikam:35304-add-delete_by_and_destroy_by branch from 5d6b79b to a1413c7 Feb 18, 2019

@abhaynikam abhaynikam force-pushed the abhaynikam:35304-add-delete_by_and_destroy_by branch from a1413c7 to fdf10ba Feb 18, 2019

@dhh

This comment has been minimized.

Copy link
Member

dhh commented Feb 18, 2019

I think we can pare back the testing a lot. This is basically just a delegator to where + destroy/delete_all. So we just need to test the delegation, not all the ways where works.

Also, should probably just spell out that this is shorthand for where().delete/destroy_all.

@abhaynikam abhaynikam force-pushed the abhaynikam:35304-add-delete_by_and_destroy_by branch from fdf10ba to 784ff51 Feb 18, 2019

@abhaynikam

This comment has been minimized.

Copy link
Contributor Author

abhaynikam commented Feb 18, 2019

@dhh : Cutoff most of the test cases and added some basic test case to check delegation. Also, updated the comments to mention shorthand for where().delete/destroy_all.

@dhh

This comment has been minimized.

Copy link
Member

dhh commented Feb 18, 2019

Looking good. Waiting for the builds to finish, but otherwise I believe good to merge.

Show resolved Hide resolved activerecord/CHANGELOG.md Outdated
Show resolved Hide resolved activerecord/CHANGELOG.md Outdated
Show resolved Hide resolved activerecord/CHANGELOG.md Outdated

@abhaynikam abhaynikam force-pushed the abhaynikam:35304-add-delete_by_and_destroy_by branch 2 times, most recently from 6501353 to 3660957 Feb 18, 2019

@abhaynikam

This comment has been minimized.

Copy link
Contributor Author

abhaynikam commented Feb 18, 2019

@kamipo : Fixed all suggestions. Thank You. 👍

# If no record is found, returns empty array.
#
# Person.all.destroy_by(name: 'Spartacus', rating: 4)
# Person.all.destroy_by("published_at < ?", 2.weeks.ago)

This comment has been minimized.

@kaspth

kaspth Feb 18, 2019

Member

This doesn’t seem to match @dhh’s description. I don’t think we want .all before a destroy_by. It should work like find_by straight on Person.


persons.destroy_by(id: [1, 2, 3])
persons.destroy_by(name: 'David')
persons.destroy_by(name: 'David', rating: 4)

This comment has been minimized.

@kaspth

kaspth Feb 18, 2019

Member

I think the plural of person is people (at the very least Rails’ inflector says so), so let’s go with that.

@abhaynikam abhaynikam force-pushed the abhaynikam:35304-add-delete_by_and_destroy_by branch 2 times, most recently from 75bbdc2 to acc507b Feb 18, 2019

@abhaynikam

This comment has been minimized.

Copy link
Contributor Author

abhaynikam commented Feb 18, 2019

@kaspth : Done with the changes. Please have a look.

@abhaynikam abhaynikam force-pushed the abhaynikam:35304-add-delete_by_and_destroy_by branch 2 times, most recently from 21cbe58 to aa13107 Feb 18, 2019

@abhaynikam

This comment has been minimized.

Copy link
Contributor Author

abhaynikam commented Feb 19, 2019

@kaspth : Fixed all the review comments.

cc/ @dhh @kamipo

assert_difference("Post.count", -3) { david.posts.destroy_by(body: "hello") }

destroyed = Author.destroy_by(id: david.id)
assert_equal [authors(:david)], destroyed

This comment has been minimized.

@abla00

abla00 Feb 19, 2019

It's easier to read:

assert_equal [david], destroyed

@abhaynikam abhaynikam force-pushed the abhaynikam:35304-add-delete_by_and_destroy_by branch from aa13107 to 5c8d4c3 Feb 19, 2019

@kamipo

kamipo approved these changes Feb 19, 2019

@kamipo kamipo merged commit 5c8d4c3 into rails:master Feb 19, 2019

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

kamipo added a commit that referenced this pull request Feb 19, 2019

Merge pull request #35316 from abhaynikam/35304-add-delete_by_and_des…
…troy_by

Introduce delete_by and destroy_by methods to ActiveRecord::Relation
@kamipo

This comment has been minimized.

Copy link
Member

kamipo commented Feb 19, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.