i in process of converting ruby on rails api on elixir , phoenix. in postgres database, have table jsonb column type. 1 of keys in json array of colors. example:
{"id": 12312312, "colors": ["red", "blue", "white"]}
what trying ecto query table records contain colors red or blue. essentially, recreate query:
select * mytable data->'colors' ?| array['red', 'blue']
i'm having difficulties constructing query ecto. here have:
note: "value" pipe delimited list of colors
def with_colors(query, value) colors = value |> string.split("|") |> enum.map(fn(x) -> "'#{x}'" end) |> enum.join(", ") # colors should "'red', 'blue'" c in query, where: fragment("data->'colors' \\?| array[?]", ^colors)) end
this not working expected. having issues replacement question mark, seems wrap additional quotes around field. proper way use fragment? or maybe there better way?
i'm going run problem again because i'm going have recreate query:
select * mytable data->'colors' @> '["red", "blue"]'
i have found solution problem.
def with_colors(query, value) colors = value |> string.split("|") c in query, where: fragment("data->'colors' \\?| ?", ^colors)) end
Comments
Post a Comment