How to convert String.split to fragment in ecto

hello, I want to create the code like this:

query = from u in BlogSchema,
        where: String.split(u.tags, ", ") in ^tag_name,

but it doesn’t work and I know we can’t use elixir macro between ecto query, I saw the fragment doc but couldn’t use it , I think it needs psql Knowledge that I don’t have it

how can I fix it?

Thanks

I think this should do it, assuming you are using Postgres:

from u in BlogSchema,
  where: fragment("? = any(string_to_array(tags, ','))", ^tag_name)

Note that in practical terms, it would be better to store the tags already split and either save them as an array in Postgres or use a join table.

3 Likes