Phoenix: Ordering a query set

穿精又带淫゛_ 提交于 2019-12-05 13:53:02

You need to use an Ecto Query:

query = from(t in Tweet, order_by: t.updated_at)
tweets = Repo.all(query)

You may want to consider defining a function in your Tweet model for the query.

def ordered(query) do
  from t in query,
    order_by: t.updated_at
end

You can also use the function syntax:

def ordered(query) do
  query
  |> order_by([t], t.updated_at)
end

Then you can use this in your controller:

tweets = Tweet |> Tweet.ordered() |> Repo.all()

Here is a good post on queries: http://blog.drewolson.org/composable-queries-ecto/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!