有没有办法通过created_at DESC列出记录,但仅适用于当前和过去的日期?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够添加附加条件以仅返回“过去”记录:
这应该显示给定框的所有锻炼,但仅显示今天或过去创建的锻炼。
You should just be able to add in the additional conditions to only return the 'past' records:
That should show all workouts for a given box, but only those that were created today or in the past.