Tuesday, April 10, 2007

Rails pagination with condition

TarcRooms.com was launched a year back and at that time all I wanted was to get the site up with the basic features. The ads was slow to come in but nowadays I get on average 10 ads per week. I guess that's good since it's serving people.

Some of the features which I initially planned that didn't make it are:
  1. Limit ads to 30 days
  2. Registration free
  3. E-mail reminders
After so long I finally got the pagination to work with condition -- that is to display ads that are no more than 30 days old.

The code for pagination is relatively simple.

def list
@classified_pages, @classifieds = paginate :classifieds,
:order => 'post_date DESC',
:per_page => 15,
:conditions => '( CURDATE() - classifieds.post_date)<30'

I avoided that from the start because I didn't want to tied the condition to MySQL's CURDATE(). Well I guess I'll be pragmatic about it and do what works for me now.

The condition above actually translated to:

SELECT (*) FROM classifieds
CURDATE() - classifieds.post_date)<30>


VK said...

Show complete content

Anonymous said...

Nice article as for me. It would be great to read a bit more concerning this matter. Thanks for sharing this information.
Sexy Lady
Escort service