开发者

Is there a way to list records by created_at DESC but only for current and past dates?

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 fo开发者_Go百科r 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜