-
Notifications
You must be signed in to change notification settings - Fork 216
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
Optimize db queries in several places #173
Conversation
ping @ryanto |
This looks good and I'm happy to merge. There are some merge conflicts, do you mind fixing them? |
b090820
to
2177f9e
Compare
Rebased. |
Awesome thanks! Looks like it's queued up here: https://travis-ci.org/github/ryanto/acts_as_votable/builds/744302627 I'll check on it in a few hours Thanks again! |
Ok I think this one is ready to be merged! If you want to rebase again (sorry!) to get the new CI running that would be helpful. |
2177f9e
to
abdccc9
Compare
Rebased. |
Thanks! Released as |
This PR broke our implementation of the gem. We have extended module ActsAsVotable
class Vote
has_many :notifications, as: :notifiable, dependent: :destroy
after_commit :create_notifications, on: [:create]
private
def create_notifications
# …
end
end
end With the changes in this PR, the associated notifications no longer get destroyed when the vote is removed. I think is due to the change of What's the recommended way to implement such functionality with this PR's new behavior? |
Do you need to remove notifications right after the vote deletion (to not show it somewhere or something like that) or just to cleanup the table? Notification.where.missing(:vote).delete_all or something like that. |
@fatkodima I need to destroy it immediately, as otherwise we might try and show a notification with a missing association somewhere. Now I could add a check to see if the vote still exists, but that would really complicate the code a lot. If there's no official solution, I'm leaning towards monkey-patching |
Got it. Yes, the monkey patching is the way to go or otherwise you can manually select and destroy notifications in the place where Something like ActiveRecord::Base.transaction do
notifications = # select notifications for user votes
user.destroy
notifications.delete_all
end |
This PR reduces and avoids db queries at several places.
Primarily this is done by replacing
COUNT..
bySELECT 1...
, using already calculated values and replacing many queries by one (like.destroy
vsdelete
).