作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 Rails 3.1 中执行 ActiveRecord 查询,我将结果分类到分组项目的子集合中,在本例中按日期分组。
我认为我的代码可以最好地解释它。这是我的方法,它有效但发出 4 个查询来完成工作。这样做似乎效率不高。
def entry_days
days = @user.entry_groups.find(
:all,
:select => 'date',
:limit => 3,
:group => 'date').map(&:date)
entry_days = days.map do |date|
{ :date => date,
:entry_groups => @user.entry_groups.find_all_by_date(date)
}
end
end
def entry_days
dates_with_entries = @user.entry_groups.find(
:all,
:select => 'date',
:limit => 3,
:group => 'date').map(&:date)
@user.entry_groups.where(:date => dates_with_entries).all.group_by(&:date).
map do |date, entry_groups|
{ :date => date,
:entry_groups => entry_groups }
end
end
dates_with_entries = user.entry_groups.all(
:select => 'date',
:limit => num_days,
:order => 'date DESC',
:group => 'date').map(&:date)
entry_groups = user.entry_groups.
where(
:date => dates_with_entries
).
all(:order => 'date DESC')
entry_days = entry_days.group_by(&:date).
map { |date, entry_groups|
{
:date => date,
:entry_groups => entry_groups
}
}
最佳答案
为什么不全部选择它们然后使用类似 group_by 的东西?
关于ruby-on-rails - Rails ActiveRecord 按日期将结果分组为子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7302992/
我以前使用过像 Netbeans 和 eclipse 这样的 IDE。 我在 friend 的电脑上下载了“Visual Studio Express 2013 for windows desktop
我正在尝试弄清楚如何在 GBA 大小的 EZ Flash 3 合 1 卡中对 PSRAM 进行编程。基本上重复 GBA Exploader 和其他程序所做的事情。 如果我选择一个 block 并对其进
Filter1=re.findall(r'',PageSource) Filter2=re.findall(r'',PageSource) Filter3=re.findall(r'(.*?).*?'
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我是一名优秀的程序员,十分优秀!