# Created\_on and updated\_on in a non-ActiveRecord model

**URL:** <https://www.ruby-forum.com/t/created-on-and-updated-on-in-a-non-activerecord-model/89120>\
**Category:** Rails\
**Created:** [March 17, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/created-on-and-updated-on-in-a-non-activerecord-model/89120 "2007-03-17T14:01:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [March 17, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/created-on-and-updated-on-in-a-non-activerecord-model/89120/1 "2007-03-17T14:01:53Z")

</div>

Hi you all,

I have a model simulating an ActiveRecord model, according to the tips  
stated in [1]. It is simulated because even though I am gonna save the  
attributes in a file, not in a DB, I still want the validations made by  
ActiveRecord::Base (see code below).

Well, the thing is that I also want the behaviour of the created\_on and  
updated\_on columns, that get modified automatically before saving.  
The problem is that I have my own “save” method and it does not work  
(remember this is not a real ActiveRecord model). I’ve tried to set the  
before\_create and before\_updated methods but they does not work (see  
code below).

Soâ€¦ is there any way to do it automatically?

Thanks.

* * *

[1]  
[http://wiki.rubyonrails.org/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord](http://wiki.rubyonrails.org/rails/pages/HowToUseValidationsWithoutExtendingActiveRecord)  
, Alternative 2.

CODE:

class PageMetadata \< ActiveRecord::Base

def self.columns()  
@columns ||= [];  
end  
def self.column(name, sql\_type = nil, default = nil, null = true)  
columns \<\< ActiveRecord::ConnectionAdapters::Column.new(name.to\_s,  
default, sql\_type.to\_s, null)  
end

column :abstract, :string  
column :title, :string  
column :created\_on, :datetime  
column :updated\_on, :datetime

validates\_presence\_of :abstract  
validates\_length\_of :abstract, :within =\> 1…80

def save(identifier)  
if self.valid?  
#I set my stuff in a fileâ€¦â€¦â€¦.  
true  
else  
false  
end  
end  
def before\_create  
self.created\_on ||=Time.now  
end  
def before\_update  
self.updated\_on ||=Time.now  
end  
end

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [March 17, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/created-on-and-updated-on-in-a-non-activerecord-model/89120/2 "2007-03-17T17:03:45Z")

</div>

Can you just use callbacks? Something like:

class PageMetadata \< ActiveRecord::Base

before\_create :set\_created\_on  
before\_update :set\_updated\_on

private

```
def set_created_on
  self.created_on ||= Time.now
end

def set_updated_on
  self.updated_on ||= Time.now
end

```

end

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [March 17, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/created-on-and-updated-on-in-a-non-activerecord-model/89120/3 "2007-03-17T17:27:04Z")

</div>

No, I can’t do that.

It’s like the callbacks didn’t work cause I am not calling to a normal  
save() method, but a save(identifier) method…  
☹

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [March 17, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/created-on-and-updated-on-in-a-non-activerecord-model/89120/4 "2007-03-17T20:19:43Z")

</div>

Um…ok…I will try this then… So this is the same reason why I also  
have to valid inside my new save() method? Cause the validations and the  
errors added has to be also managed by myself ☹

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [March 17, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/created-on-and-updated-on-in-a-non-activerecord-model/89120/5 "2007-03-17T18:50:39Z")

</div>

On Mar 17, 12:27 pm, Damaris F. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> No, I can’t do that.
> 
> It’s like the callbacks didn’t work cause I am not calling to a normal  
> save() method, but a save(identifier) method…  
> ☹

You are overwriting the ‘save’ method entirely, which overwrites the  
original behavior of save. The way ActiveRecord works the save method  
will check to see if you are saving a new record or updating an  
original, by calling “create\_or\_update”.

If you are creating a new record the ‘create’ method is called. This  
method is actually aliased to ‘create\_with\_callbacks’, which invokes  
the callback stack for before\_create, etc.

You can specify the callbacks you want to use in the following  
example:

def save(identifier)  
if valid?  
callback( :before\_create )  
# … write to disk  
callback( :after\_create )  
end  
#… other stuff…  
end

OR you can overwrite the create/callback chain:

class YourModel  
def create  
# YOUR CREATE FUNCTIONALITY HERE  
end

alias\_method\_chain :create, :callbacks  
end

Now when you call save, your “create” method will be used over  
ActiveRecord’s. Now you don’t even have to overrirde save, because you  
can put your “save” logic in the create method. Likewise you’d want to  
do the same for the “update” and “destroy” methods as well.

Zach  
[http://www.continuousthinking.com](http://www.continuousthinking.com)
