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. -- Posted via http://www.ruby-forum.com/.
Jasbur
2006-Jun-14 04:35 UTC
[Rails] Re: Using now() to determine what should be displayed
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. -- Posted via http://www.ruby-forum.com/.
Francois Beausoleil
2006-Jun-14 04:38 UTC
[Rails] Using now() to determine what should be displayed
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 ! -- Fran?ois Beausoleil http://blog.teksol.info/
Steve Longdo
2006-Jun-14 04:47 UTC
[Rails] Using now() to determine what should be displayed
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 On 6/13/06, Jasbur <jasbur@gmail.com> wrote:> > 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. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060614/d2b149a6/attachment-0001.html
Bill Walton
2006-Jun-14 13:47 UTC
[Rails] Re: Using now() to determine what should be displayed
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