【发布时间】:2018-02-19 01:02:11
【问题描述】:
我有 position_positions 表,其中 JSONB 列名为 data。我的 lambda 有点卡住了,它应该按 ptype 的 data 列的值进行过滤。
我有两个参数的 lambda(这是强制性的)的方法如下所示:
def find_ptype
-> (column, formated_value) {
where("position_positions.data->>'ptype' = ?", "#{formated_value}%")
}
end
但是我得到了这个错误:NoMethodError (undefined method "where" for #
另一个尝试是与 Arel:
def find_ptype
->(column, formatted_value) {
Arel.sql("JSON_EXTRACT(#{column.field}, '$.ptype') = '#{formatted_value}'")
}
end
但我收到此错误:
ActiveRecord::StatementInvalid (PG::UndefinedFunction: ERROR: function json_extract(text, unknown) does not exist
LINE 1: ...ND "position_positions"."deleted_at" IS NULL AND (JSON_EXTRA...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
: SELECT COUNT(*) FROM "position_positions" WHERE "position_positions"."type" IN ('Position::Outdoor') AND "position_positions"."deleted_at" IS NULL AND (JSON_EXTRACT(data->>'ptype', '$.ptype') = 'billboard')):
在上面的这个例子中,我试图为我的data->>'ptype' 传递billboard 值,但是得到了那个错误。
我该如何修正我的方法来过滤我的 JSONB 列值?我会很高兴得到任何提示。谢谢。
【问题讨论】:
标签: ruby-on-rails ruby postgresql lambda