# Using now() to determine what should be displayed

**URL:** <https://www.ruby-forum.com/t/using-now-to-determine-what-should-be-displayed/95805>\
**Category:** Rails\
**Created:** [June 14, 2006, 6:31am UTC](https://www.ruby-forum.com/t/using-now-to-determine-what-should-be-displayed/95805 "2006-06-14T06:31:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 14, 2006, 6:31am UTC](https://www.ruby-forum.com/t/using-now-to-determine-what-should-be-displayed/95805/1 "2006-06-14T06:31:00Z")

</div>

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:

# Posts ending soon

\<% for post in @posts do %\> **Post Title:** \<%= post.title %\> **Date Ending:** \<%= post.end\_date %\> \<%= link\_to 'View this post', :action =\> 'show', :id =\> post %\> \<% end %\>

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.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 14, 2006, 6:35am UTC](https://www.ruby-forum.com/t/using-now-to-determine-what-should-be-displayed/95805/2 "2006-06-14T06:35:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 14, 2006, 6:39am UTC](https://www.ruby-forum.com/t/using-now-to-determine-what-should-be-displayed/95805/3 "2006-06-14T06:39:34Z")

</div>

Hi,

2006/6/14, Jasbur [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> @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 !

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 14, 2006, 6:49am UTC](https://www.ruby-forum.com/t/using-now-to-determine-what-should-be-displayed/95805/4 "2006-06-14T06:49:12Z")

</div>

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:

# Posts ending soon

\<% for post in @posts do %\> \<% if post.end\_date \> now %\> **Post Title:** \<%= post.title %\> **Date Ending:** \<%= post.end\_date %\> \<%= link\_to 'View this post', :action =\> 'show', :id =\> post %\>  
  
\<% end %\>\<% end %\>

I like #1 the best it keeps the view as simple.  
-Steve  
[http://www.stevelongdo.com](http://www.stevelongdo.com)

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 14, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/using-now-to-determine-what-should-be-displayed/95805/5 "2006-06-14T15:49:06Z")

</div>

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/](http://api.rubyonrails.org/) for ‘to\_date’

hth,  
Bill
