[英]Getting a max/min date value from an associated model
Rails 2.3.5导轨 2.3.5
Below, (tickets:have_many => logs), I'm just listing the 'created_at' value of index 0. What I'd like to get is the max & min values for that field (saying something like 'first log was 07-01-2011, last log entry 07-18-2011')下面,(tickets:have_many => logs),我只是列出索引 0 的“created_at”值。我想得到的是该字段的最大值和最小值(比如“第一个日志是 07 -01-2011,最后一个日志条目 07-18-2011')
<% @tickets.each do |t| %>
<% if !t.log_entries[0].nil? %>
<%= t.log_entries[0].created_at %>
<% end %>
<% end %>
<% end %>
Is there an easy way to do this?是否有捷径可寻? I tried playing around with Max but couldn't figure out a working syntax.我尝试使用 Max,但无法找出有效的语法。 The only other thing I can thing of is to do a find_first asc & desc query in the view.我唯一能做的就是在视图中执行 find_first asc & desc 查询。
Thanks !谢谢 !
You can easily do this by a Rails subquery :您可以通过Rails 子查询轻松做到这一点:
From <%= @ticket.log_entries.find(:first, :order => "created_at ASC").created_at.to_s %>
to <%= @ticket.log_entries.find(:first, :order => "created_at DESC").created_at.to_s %>
Happy coding:)快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.