Expiring items


#1

In my database I have a products table that has a bunch of attributes
(title, description, etc) including one for date_expires.

Is there a best practices method to get the database to erase the
expired items? Is that done outside RoR via Cron or something?

Else is it better to just have my app filter out anything that is
expired but still keep those records in my dbase?


#2

I prefer to filter the lists and then provide an “administrative”
function that runs the proper command for deleting expired items. It’s
part of my CYA philosophy… Always have the user process destructive
actions and only after a confirmation form. :slight_smile: That way if they do
call you to ask about getting the data back you can say “sorry” in a
much nicer way.

You should be able to add :conditions to your find method:

:conditions => “date_expires >= now()”

And of course, your delete would just find with the reverse (<= now()).
I also like to toss in the confirmation page a “are you sure you’d like
to delete these (count) items” message just so they realize how
destructive the command will be.


#3

:conditions => “date_expires >= now()”

I thought about what you said and agree. Instead of just purging them
I’d like to split my list method into 2 sides:

Current, which shows all the items belonging to the user that have not
expired

and a second section called ‘Expired’

which would show the expired items.

I thought about putting the conditions => “date_expires >= now()” in my
list view but that did not seem to work. How should one go about doing
it correctly?

-Vince


#4

How about creating a couple of method in the model?

At there most simple that would be something like
def self.expired
find(:all, conditions => [“date_expires >Time.now”]
end

def self.current
find(:all, conditions => [“date_expires <=Time.now”]
end

Then call it with mymodel.expired etc

I think that’s prob the correct MVC way of doing it, but I’m still quite
new with the whole MVC thing so perhaps take this under advisement


#5

Actually, I think I worked out a way to do it between the controller and
view (still not very familiar with the model but could not seem to get
it involved…)

What I need to do is apply two conditions though:

@product_pages, @products = paginate :products, :per_page => 10,
                            :conditions => ['user_id = ?', @user.id]

and then add a second condition for [‘date_expires >= now()’]

I can’t seem to get that syntax worked out somehow. When I try:

@product_pages, @products = paginate :products, :per_page => 10,
                            :conditions => ['user_id = ?', 

@user.id],
:conditions => ['date_expires <= now()

It ends up giving me items for all users (only 2nd condition) as does
this:

@product_pages, @products = paginate :products, :per_page => 10,
                            :conditions => ['user_id = ?', @user.id] 

&&
[‘date_expires <= now()’]

How can I get it to apply BOTH conditions to @products and only show
products for the current user that are not expired?


#6

Ok, I think I solved it with this line:

@product_pages, @products = paginate :products, :per_page => 10,
:conditions => [‘user_id = ? AND date_expires >= ?’, @user.id,
Time.now]


#7

If resources aren’t a problem, then generally speeking I’d keep all
the data. By all means have a method which only admins can access for
purging the data, but I wouldn’t use it unless my database size became
a problem. This way you as an admin can get at expired data for your
records, if the need should arise. You could even let the admin choose
when to purge from (ie, delete all records that expired more than
three weeks ago). Just my opinion.
-N


#8

Duplicating the :conditions just results in overwriting previous
conditions.

On Monday, April 24, 2006, at 1:27 AM, Vince W. wrote:

@product_pages, @products = paginate :products, :per_page => 10,
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

_Kevin