Forum: Ruby on Rails Using now() to determine what should be displayed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jasbur (Guest)
on 2006-06-14 06:31
I'm trying to create a paginated list that will take note of the
"end_date" field in my table. This is what I have:

@post_pages, @posts = paginate(:posts, :per_page => 10, :order_by =>
'end_date')

in the controller. And for the view:

<h1>Posts ending soon</h1>
<ul><% for post in @posts do %>
<strong>Post Title:  </strong><%= post.title %> <strong> Date Ending:
</strong><%= post.end_date %>    <%= link_to 'View this post', :action
=> 'show', :id => post %></br>
</br>
<% end %></ul>

Everything works great. But, I would like anything with an "end_date"
before today to not be displayed. I'm assuming I need to weasel now() in
there someplace, but I can't figure out where.
Jasbur (Guest)
on 2006-06-14 06:35
I also tried:

@ending = Post.find(:all, :conditions => "end_date <= now()")
  @post_pages, @ending = paginate(:posts, :per_page => 10, :order_by =>
'end_date')

in the controller, no luck.
8d4966c3e834dc2fb6900c5fff7c7ea5?d=identicon&s=25 François B. (fbeausoleil)
on 2006-06-14 06:39
(Received via mailing list)
Hi,

2006/6/14, Jasbur <jasbur@gmail.com>:
> @post_pages, @posts = paginate(:posts, :per_page => 10, :order_by =>
> 'end_date')

@post_pages, @posts = paginate(:posts, :per_page => 10,  :conditions
=> 'end_date >= NOW()':order_by => 'end_date')

Your other solution would not work because you'd be creating a
separate array.  If you were to check log/development.log you'd see
the two queries.

Hope that helps !
Steve Longdo (Guest)
on 2006-06-14 06:49
(Received via mailing list)
1) In your controller add conditions to the paginate call:
@post_pages, @posts = paginate(:posts, :conditons => 'end_date >
CURRENT_DATE',:per_page => 10, :order_by =>
'end_date')
---alternatively---
2)In the view you could do the following:
<h1>Posts ending soon</h1>
<ul><% for post in @posts do %>
<% if post.end_date > now %>
<strong>Post Title:  </strong><%= post.title %> <strong> Date Ending:
</strong><%= post.end_date %>    <%= link_to 'View this post', :action
=> 'show', :id => post %></br>
</br>
<% end %><% end %></ul>

I like #1 the best it keeps the view as simple.
-Steve
http://www.stevelongdo.com
Bill Walton (Guest)
on 2006-06-14 15:49
(Received via mailing list)
Jasbur wrote:


>I also tried:
>
> @ending = Post.find(:all, :conditions => "end_date <= now()")
>  @post_pages, @ending = paginate(:posts, :per_page => 10, :order_by =>
> 'end_date')
>
 Try "end_date => Time.now"

If you need it in a specific Date format, check the method documentation
at
http://api.rubyonrails.org/ for 'to_date'

hth,
Bill
This topic is locked and can not be replied to.