# Expiring items

**URL:** <https://www.ruby-forum.com/t/expiring-items/94148>\
**Category:** Rails\
**Created:** [April 22, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/expiring-items/94148 "2006-04-22T23:03:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 22, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/expiring-items/94148/1 "2006-04-22T23:03:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 23, 2006, 4:46am UTC](https://www.ruby-forum.com/t/expiring-items/94148/2 "2006-04-23T04:46:30Z")

</div>

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. 🙂 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.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 23, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/expiring-items/94148/3 "2006-04-23T23:18:20Z")

</div>

> :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

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 24, 2006, 12:32am UTC](https://www.ruby-forum.com/t/expiring-items/94148/4 "2006-04-24T00:32:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 24, 2006, 1:27am UTC](https://www.ruby-forum.com/t/expiring-items/94148/5 "2006-04-24T01:27:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 24, 2006, 2:15am UTC](https://www.ruby-forum.com/t/expiring-items/94148/6 "2006-04-24T02:15:24Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 24, 2006, 1:52am UTC](https://www.ruby-forum.com/t/expiring-items/94148/7 "2006-04-24T01:52:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 24, 2006, 2:50am UTC](https://www.ruby-forum.com/t/expiring-items/94148/8 "2006-04-24T02:50:28Z")

</div>

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](http://lists.rubyonrails.org/mailman/listinfo/rails)

\_Kevin
