PERF: Avoid running query unnecessarily when updating bookmark. (#14276)

* Avoid loading an entire ActiveRecord object when saving and updating.
* Avoid running a DB query when `post_id` or `user_id` is not changed.
This commit is contained in:
Alan Guo Xiang Tan
2021-09-09 08:50:26 +08:00
committed by GitHub
parent 13b31def80
commit 1e05175364
2 changed files with 24 additions and 8 deletions

View File

@@ -31,7 +31,10 @@ class Bookmark < ActiveRecord::Base
if: -> { reminder_type.present? }
}
validate :unique_per_post_for_user
validate :unique_per_post_for_user,
on: [:create, :update],
if: Proc.new { |b| b.will_save_change_to_post_id? || b.will_save_change_to_user_id? }
validate :ensure_sane_reminder_at_time
validate :bookmark_limit_not_reached
validates :name, length: { maximum: 100 }
@@ -47,9 +50,9 @@ class Bookmark < ActiveRecord::Base
end
def unique_per_post_for_user
existing_bookmark = Bookmark.find_by(user_id: user_id, post_id: post_id)
return if existing_bookmark.blank? || existing_bookmark.id == id
self.errors.add(:base, I18n.t("bookmarks.errors.already_bookmarked_post"))
if Bookmark.exists?(user_id: user_id, post_id: post_id)
self.errors.add(:base, I18n.t("bookmarks.errors.already_bookmarked_post"))
end
end
def ensure_sane_reminder_at_time