patrickdavey
Help with passing an array (charlist) to a fragment
I’m trying to order by a specific list of IDs (dynamicall). I went down the route of trying to build up a case statement but found that difficult, and then I found this stackoverflow which suggested using ORDER BY array_position(ARRAY['f', 'p', 'i', 'a']::varchar[], x_field)
So I tried that in my fragment: |> order_by([p, pt], fragment("array_position(ARRAY[?]::int[], ?)", ^ids, p.id)) but my ids are being interpreted as a charlist [88], and I imagine this isn’t a valid way to pass data around perhaps? I also tried building up a string of “id,id” and using string_to_array but didn’t have luck there either.
Is there something I’m missing..or is this approach just flawed from the start.
First Post!
krasenyp
I believe you need to do array_position(?::int[]…. Currently you’re nesting a list inside an array and of course Ecto will treat your ids as a list of characters.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










