Ash FrameworkAF
Ash Framework3y ago
23 replies
Jason

Sorting on a field in a relation

I tried the solution from https://discordapp.com/channels/711271361523351632/1072944619169534063, but can't seem to get it to work. Am I missing something?

tweet.ex
    read :feed do
      description "Get the feed of tweets for a user"

      argument :user_id, :uuid do
        allow_nil? false
      end

      prepare build(load: [:items], sort: [item_order: :asc])

      filter expr(visible_to(user_id: arg(:user_id)))
    end
-----------------
  relationships do
    has_many :items, Tweet.Tweets.Item
  end
---------------
  aggregates do
    first :item_order, :items, :row_order


item.ex
  attributes do
    attribute :row_order, :integer do
      allow_nil? true
    end


liveview file
  defp assign_twees(socket) do
    tweets =
      Tweets.Tweet.feed!(socket.assigns.current_user.id,
        load: tweet_load(socket.assigns.current_user)
      )

  ...

  defp tweet_load(current_user) do
    [
      :author_email,
      :author,
       items: [
        :sum_item_count,
      ]
    ]
  end
Discord
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Was this page helpful?