简体   繁体   English

Ruby on Rails如何获取最后一个查询?

[英]Ruby on Rails how to get last query?

I'm having trouble trying to figure out when I reached the end of my query. 我无法弄清楚何时到达查询末尾。 So what I want to do is list all the records in my database that begin with the letter A which I got however I want to output a message if the query turns out blank. 所以我想做的是列出数据库中所有以字母A开头的记录,但是如果查询结果为空,我想输出一条消息。 When I try I get a bunch of my custom messages even the query didn't turn out blank. 当我尝试时,甚至收到一堆我的自定义消息,但查询并没有变成空白。 Is there any way to tell if I've reached EOF in ruby on rails? 有什么方法可以判断我是否已经达到了红宝石的EOF?

Sample 样品

    <div id = "content-A">
                <p>A</p>
                <% @animes.each do |anime| %>
                    <% if anime.aname.starts_with?('A') %>
                        <%= link_to anime.aname, {:action => 'list'} %>
                    <% else %>
                        <p>No anime listed in this Category :( </p>
                    <%end%>
                <%end %>
            </div>

I believe you want sth like: 我相信你想……

<% animes_group = @animes.group_by {|anime| anime.aname.to_s[0].upcase}
  ('A'..'Z').each do |letter| %>
  <div id="content-<%= letter %>">
  <p><%= letter %></p>
  <% if animes = animes_group[letter] %>
    <% animes.each do |anime| %>
      <%= link_to anime.aname, {:action => 'list'} %>
    <% end %>
  <% else %>
    <p>No anime listed in this Category :( </p>
  <%end%>
<% end %>

You should consider moving some of the logic to the controller here, however what is to be moved depends on many factors like whether @animes are being used anywhere else etc. 您应该在此处考虑将一些逻辑移至控制器,但是要移走的逻辑取决于许多因素,例如是否在其他地方使用了@animes等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM