# Setting expiration date based on creation date in model?

**URL:** <https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739>\
**Category:** Rails\
**Created:** [June 30, 2008, 9:20am UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739 "2008-06-30T09:20:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 9:20am UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/1 "2008-06-30T09:20:16Z")

</div>

Hi,

I am handling some records that have expiration date. Records should  
expire 30 days after its creation.

I could add the logic in the controller but I think that should/could be  
done in the model.

How can I do that in the model?  
Why is it better in the model than in the controller?

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 11:50am UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/2 "2008-06-30T11:50:30Z")

</div>

On 30 Jun 2008, at 08:20, comopasta Gr wrote:

> How can I do that in the model?  
> def self.expire!  
> expirable = find :all, :conditions =\> [“created\_at \< ?”, 30.days.ago]

# do something with expirable

end

> Why is it better in the model than in the controller?

Because it’s none of your controller’s business.

Fred

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/3 "2008-06-30T12:32:49Z")

</div>

Thank you Fred!

Excuse my ignorance but is the function called automatically?  
I’ve seen the ! ending in some functions, I wonder what is the meaning  
of it.

I have a couple of books on RoR, I think I need new ones already  
covering more stuff right now 😉

Thanks!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/4 "2008-06-30T12:45:02Z")

</div>

On 30 Jun 2008, at 11:32, comopasta Gr wrote:

> Thank you Fred!
> 
> Excuse my ignorance but is the function called automatically?

Nope

> I’ve seen the ! ending in some functions, I wonder what is the meaning  
> of it.

Whatever meaning it has is usually a convention (eg on String methods  
with a ! modify inplace, the ones without return a copy with that  
modification (eg gsub and gsub!).  
As far as ruby itself is concerned it makes no difference  
Fred

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/5 "2008-06-30T12:50:46Z")

</div>

> > …is the function called automatically?
> 
> Nope

Sorry again, so from where and by who should that function be called? A  
cron job for example?

Thanks once again.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 1:21pm UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/6 "2008-06-30T13:21:59Z")

</div>

On 30 Jun 2008, at 11:50, comopasta Gr wrote:

> > > …is the function called automatically?
> > 
> > Nope
> 
> Sorry again, so from where and by who should that function be  
> called? A  
> cron job for example?

For example. I didn’t say anything about that because I haven’t the  
faintest clue when you want it called.

Fred

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/7 "2008-06-30T22:12:55Z")

</div>

Thanks guys. Looks clear now.

Cheers.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [June 30, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/setting-expiration-date-based-on-creation-date-in-model/141739/8 "2008-06-30T14:11:04Z")

</div>

expirable = find :all, :conditions =\> [“created\_at \< ?”, 30.days.ago]

# do something with expirable

```
 This u can write as a rake task and can be called it from cron

```

Sijo
