使用不带列的postgres计算值作为Ruby变量
我有一个Hanami web应用程序,其中一个功能是将数据库中的字符串与用户提供的字符串进行比较。为此,我使用了postgres扩展pg_trgm。Ruby中转换后的查询条件如下所示:使用不带列的postgres计算值作为Ruby变量,ruby,postgresql,hanami,hanami-model,rom-rb,Ruby,Postgresql,Hanami,Hanami Model,Rom Rb,我有一个Hanami web应用程序,其中一个功能是将数据库中的字符串与用户提供的字符串进行比较。为此,我使用了postgres扩展pg_trgm。Ruby中转换后的查询条件如下所示: 。其中{相似性(:内容、源文本用于查找)>模拟分数限制} 我需要解决的问题是将计算出的相似性分数返回到Ruby,并将其呈现给用户。然而,相似性分数不是一个属性,因为它只是相关的,或者只有在PG中调用函数来比较两个字符串时才应该存在 有办法做到这一点吗 编辑2021 我对回购部分的实施做了如下更改: def fi
。其中{相似性(:内容、源文本用于查找)>模拟分数限制}
我需要解决的问题是将计算出的相似性分数返回到Ruby,并将其呈现给用户。然而,相似性分数不是一个属性,因为它只是相关的,或者只有在PG中调用函数来比较两个字符串时才应该存在
有办法做到这一点吗
编辑2021 我对回购部分的实施做了如下更改:
def find_by_segment_match(source_text_for_lookup, source_lang, sim_score)
aggregate(:translation_records)
.where(language_id: source_lang)
.where { similarity(:content, source_text_for_lookup) > sim_score/100.00 }
.select_append { float::similarity(:content, source_text_for_lookup).as(:similarity) }
.order { similarity(:content, source_text_for_lookup).desc }
end
对此
def find_by_segment_match1(source_text_for_lookup, source_lang, sim_score)
segments
.where(language_id: source_lang)
.where { similarity(:content, source_text_for_lookup) > sim_score/100.00 }
.select_append { float::similarity(:content, source_text_for_lookup).as(:similarity) }
.order { similarity(:content, source_text_for_lookup).desc }
end
所以我删除了聚合,只是现在,select_append不再返回作为段记录一部分的计算值。为什么会发生变化
可以看到输出
编辑结束
注意,Sebastjan至少在ROM中,您可以附加函数调用,如smth
。选择\u append{float::similarity(:content,source\u text\u用于查找)。as(:similarity)}
生成的结构应该具有
相似性
属性和您需要的值。这非常有效。我只需要删除到特定实体的映射。这是预期的行为吗?