有没有办法通过created_at DESC列出记录,但仅适用于当前和过去的日期?

发布于 2024-10-06 17:37:50 字数 461 浏览 0 评论 0原文

我有一个 Ruby on Rails 应用程序,允许用户创建一个 Box(健身房),然后创建 Workouts,以便用户可以看到他们当天要做什么。

Box has_many :workouts
Workout belongs_to :box

我已让用户能够编辑锻炼.created_at 字段并允许他们输入未来日期的锻炼。在 /view/boxes/show.html.erb 页面上,我只想显示当前日期和过去的锻炼。所以本质上我想列出 created_at DESC 的所有锻炼,但我不想要未来的锻炼。有没有办法在控制器操作中做到这一点?

@workouts = @box.workouts.all(:order => "created_at DESC")

如果没有,最好的方法是什么?

I have a Ruby on Rails application that allows for users to create a Box (gym) and then create Workouts so that users can see what they will be doing that day.

Box has_many :workouts
Workout belongs_to :box

I have given the user the ability to edit the workout.created_at field and allow them to enter workouts for future dates. On the /view/boxes/show.html.erb page, I only want to display Workouts that are for the present date and in the past. So essentially I want to list all workouts by created_at DESC but I don't want future Workouts. Is there a way to do this in the controller action?

@workouts = @box.workouts.all(:order => "created_at DESC")

If not, what is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-10-13 17:37:50

您应该能够添加附加条件以仅返回“过去”记录:

@workouts = @box.workouts.all(:order => "created_at DESC", :conditions => ["created_at <= ?", Time.now])

这应该显示给定框的所有锻炼,但仅显示今天或过去创建的锻炼。

You should just be able to add in the additional conditions to only return the 'past' records:

@workouts = @box.workouts.all(:order => "created_at DESC", :conditions => ["created_at <= ?", Time.now])

That should show all workouts for a given box, but only those that were created today or in the past.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文