![](/img/trans.png)
[英]How to use reduce/inject in Ruby without getting Undefined variable
[英]Use a postgres computed value without a column as a Ruby variable
我有一個 Hanami web 應用程序,其中一個功能是將數據庫中的字符串與用戶提供的字符串進行比較。 為此,我使用了 postgres 擴展 pg_trgm。 Ruby 中的翻譯查詢條件如下所示:
.where {similarity(:content, source_text_for_lookup) > sim_score_limit}
我需要解決的問題是將計算出的相似度得分返回給 Ruby 並將其呈現給用戶。 但是,相似度分數不是一個屬性,因為它僅在 PG 中調用 function 以比較兩個字符串時才相關或應該存在。
有沒有辦法做到這一點?
編輯 2021
我已經對 Segment repo 中的實現進行了更改:
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 不再返回計算值作為段記錄的一部分。 為什么會發生這種變化?
output 可以在這里看到。
編輯結束
問候,塞巴斯蒂安
至少在 ROM 中你可以 append function 調用,smth like
.select_append { float::similarity(:content, source_text_for_lookup).as(:similarity) }
生成的結構應該具有與您需要的值similarity
的屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.