4
我是[一个noob]玩弄凤凰框架的乐趣和建立一个小的推特克隆。我的一切工作,但是,我想按updated_at
字段排序(升序)。正如你可以从tweet_controller看到的那样,我尝试过使用order_by子句,但是这对我没有任何帮助。Phoenix:订购查询集
问题
如何实现这一目标?在EEx内还是在tweet_controller本身内?
鸣叫/ index.html.eex
<div class="row">
<%= for tweet <- @tweets do %>
<h4><%= tweet.tweet %></h4>
<% end %>
</div>
控制器/ tweet_controller.ex
...
alias TodoApp.Tweet
def index(conn, _params) do
tweets = Repo.all(Tweet, order_by: tweet)
render(conn, "index.html", tweets: tweets)
end
...
只是为了确认,这不能用EEx来完成? – plotplot
@plotplot这是属于数据库的东西(因为在执行查询之前数据库会进行排序,这对于分页很重要)。即使你确实想用Enum.sort进行排序,我也会执行这种排序控制器中的东西,而不是模板。 – Gazler