feat: 類似度算出バッチ修正,ほか(#228) (#232)
#228 #228 #228 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #232
This commit was merged in pull request #232.
This commit is contained in:
@@ -9,12 +9,7 @@ class Post < ApplicationRecord
|
||||
has_many :post_tags_with_discarded, -> { with_discarded }, class_name: 'PostTag'
|
||||
has_many :tags, through: :active_post_tags
|
||||
has_many :user_post_views, dependent: :destroy
|
||||
has_many :post_similarities_as_post,
|
||||
class_name: 'PostSimilarity',
|
||||
foreign_key: :post_id
|
||||
has_many :post_similarities_as_target_post,
|
||||
class_name: 'PostSimilarity',
|
||||
foreign_key: :target_post_id
|
||||
has_many :post_similarities
|
||||
has_one_attached :thumbnail
|
||||
|
||||
before_validation :normalise_url
|
||||
@@ -34,18 +29,12 @@ class Post < ApplicationRecord
|
||||
end
|
||||
|
||||
def related(limit: nil)
|
||||
ids_with_cos =
|
||||
post_similarities_as_post.select(:target_post_id, :cos)
|
||||
.map { |ps| [ps.target_post_id, ps.cos] } +
|
||||
post_similarities_as_target_post.select(:post_id, :cos)
|
||||
.map { |ps| [ps.post_id, ps.cos] }
|
||||
ids = post_similarities.select(:target_post_id).order(cos: :desc)
|
||||
ids = ids.limit(limit) if limit
|
||||
ids = ids.pluck(:target_post_id)
|
||||
return [] if ids.empty?
|
||||
|
||||
sorted = ids_with_cos.sort_by { |_, cos| -cos }
|
||||
|
||||
ids = sorted.map(&:first)
|
||||
ids = ids.first(limit) if limit
|
||||
|
||||
Post.where(id: ids).index_by(&:id).values_at(*ids)
|
||||
Post.where(id: ids).order(Arel.sql("FIELD(id, #{ ids.join(',') })"))
|
||||
end
|
||||
|
||||
def resized_thumbnail!
|
||||
|
||||
Reference in New Issue
Block a user