gpt4 book ai didi

ruby-on-rails - 检查日期是否在日期范围内

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:41 25 4
gpt4 key购买 nike

我有一个这样创建的对象数组:

all_project_assignments = ProjectAssignment.where(active: true, hour_type_id: 1)

现在我想使用这个结果数组来只选择日期范围内的作业。日期范围是:

date_range = @current_month.at_beginning_of_month..@current_month.at_end_of_month

并且 all_project_assignments 的每个成员都有必须在范围内的字段 entry_date。

我的选择是:

project_assignments = ProjectAssignment.where(active: true, hour_type_id: 1, entry_date: date_range)

但我不想要另一个数据库查询。那么,执行此操作的最佳方法是什么?

最佳答案

您可以将查询链接在一起:

all_project_assignments = ProjectAssignment.where(active: true, hour_type_id: 1)
date_range = @current_month.at_beginning_of_month..@current_month.at_end_of_month
project_assignments = all_project_assignments.where(entry_date: date_range)

但是,这将再次查询数据库。

如果您想过滤检索到的行,只需执行

project_assignments = all_project_assignments.select do |assignment|
assignment.entry_date.between?(@current_month.at_beginning_of_month, @current_month.at_end_of_month)
end

关于ruby-on-rails - 检查日期是否在日期范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25913604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com