Ruby on rails 钢轨相似性
我正在尝试在我的rails应用程序中实现一个类似的posts功能。为此,我的posts控制器中有这段代码Ruby on rails 钢轨相似性,ruby-on-rails,similarity,Ruby On Rails,Similarity,我正在尝试在我的rails应用程序中实现一个类似的posts功能。为此,我的posts控制器中有这段代码 @related_Posts = Post .where('posts.id != ?', @post.id) .where(:post_title=>@post.post_title) .where(:category_id=>@post.category).limit(5) 这工作很好,但是我想知道,如果只有50%的标题是相似的,而不是完整的标题,
@related_Posts = Post
.where('posts.id != ?', @post.id)
.where(:post_title=>@post.post_title)
.where(:category_id=>@post.category).limit(5)
<>这工作很好,但是我想知道,如果只有50%的标题是相似的,而不是完整的标题,使用一个类似你想要的全文搜索引擎,那么有没有一种方法可以考虑一篇文章。可以将其配置为在标题字段中查找具有类似单词的相关帖子。只需使用sql
类似的
函数,在rails(匹配
)中,它看起来如下所示:
class Post < ActiveRecord::Base
scope :by_not_ids, ->(ids = []) { where.not id: ids }
scope :by_title_like, ->(title = '') {
arel_table[:title].matches("%#{title}%")
}
scope :by_category_ids, ->(category_ids = []) {
where category_id: category_ids
}
end
class PostsController < ApplicationController
def index
@related_posts = related_scoping
end
private
def related_scoping
Post.by_not_ids([@post.id])
.by_title_like(@post.title)
.by_category_ids([@post.category_id])
.limit(5)
end
end
class Post(ids=[]){where.not id:ids}
范围:由_title _like,->(title=''){
arel_表[:title]。匹配项(“%#{title}%”)
}
范围:按类别ID,->(类别ID=[]){
其中category\u id:category\u id
}
结束
类PostsController<应用程序控制器
def索引
@相关职位=相关范围
结束
私有的
def相关_范围界定
Post.by_not_id([@Post.id])
.by_title_like(@post.title)
.by_category_id([@post.category_id])
.限额(5)
结束
结束
我只想在显示页面中显示与博客相关的帖子did@userails我想这会满足你的要求。你如何定义相似性?