optimization: prefer relation.none over relation.where("false")

Using `relation.none` instead of `relation.where("false")` avoids an sql query.
This commit is contained in:
evazion
2018-08-23 15:21:51 -05:00
parent db23e06a4f
commit 85ae2cda0d
4 changed files with 5 additions and 5 deletions

View File

@@ -14,7 +14,7 @@ module PostSets
query query
end end
rescue ::Post::SearchError rescue ::Post::SearchError
::Post.where("false") ::Post.none
end end
def presenter def presenter

View File

@@ -64,7 +64,7 @@ class Comment < ApplicationRecord
end end
def for_creator(user_id) def for_creator(user_id)
user_id.present? ? where("creator_id = ?", user_id) : where("false") user_id.present? ? where("creator_id = ?", user_id) : none
end end
def for_creator_name(user_name) def for_creator_name(user_name)

View File

@@ -22,7 +22,7 @@ class PostArchive < ApplicationRecord
if user_id if user_id
where("updater_id = ?", user_id) where("updater_id = ?", user_id)
else else
where("false") none
end end
end end

View File

@@ -76,7 +76,7 @@ class PostFlag < ApplicationRecord
q = q.where.not(post_id: CurrentUser.user.posts) q = q.where.not(post_id: CurrentUser.user.posts)
q = q.where("creator_id = ?", params[:creator_id].to_i) q = q.where("creator_id = ?", params[:creator_id].to_i)
else else
q = q.where("false") q = q.none
end end
end end
@@ -86,7 +86,7 @@ class PostFlag < ApplicationRecord
q = q.where.not(post_id: CurrentUser.user.posts) q = q.where.not(post_id: CurrentUser.user.posts)
q = q.where("creator_id = ?", flagger_id) q = q.where("creator_id = ?", flagger_id)
else else
q = q.where("false") q = q.none
end end
end end