【发布时间】:2017-08-16 04:41:16
【问题描述】:
我正在尝试创建一个 Rails 控制器查询,以查找不是将来的列中最新日期的记录(这就是为什么我不能执行简单的order 语句的原因)。我找到了this post,但那里的答案 (MyModel.where("created_at < ?", 2.days.ago)) 引发了一些错误。
我的这个工作正常,但如果有一周开始在未来,它会选择那个,而不是过去最近的一周:
@most_recent = Plan.order("week_starting").last
关于如何在 Rails 中正确执行此操作有什么想法吗?
这是我的整个plans#index 方法:
def index
@plans = Plan.where(user_id: current_user.id)
@plan = Plan.new
@recent = Plan.order("week_starting").last <<<THIS LINE
@recipes = Recipe.where(user_id: current_user.id)
@monday = Recipe.where(id: @recent.monday)[0]
@tuesday = Recipe.where(id: @recent.tuesday)[0]
@wednesday = Recipe.where(id: @recent.wednesday)[0]
@thursday = Recipe.where(id: @recent.thursday)[0]
@friday = Recipe.where(id: @recent.friday)[0]
@saturday = Recipe.where(id: @recent.saturday)[0]
@sunday = Recipe.where(id: @recent.sunday)[0]
end
基本上,我需要当前用户的plan,它的week_starting 值最接近Date.today,但仍在过去。
【问题讨论】:
标签: ruby-on-rails activerecord