Automatic created_at and updated_at


#1

I see in the rom-rails there is a way to do this, I am not on rails fortunately.

What would be the best way to have these fields and have them populated and the updated_at field populated on save? at the moment I am manually doing this in my models to_h method, and then using this to persist for example:

class User

def to_h
{

updated_at: Time.now,
created_at: @created_at || Time.now
}
end
end

Which is a bit clunky for me and I could not find any examples out there demonstrating a solution.


#2

Hey Stefan,

Sorry for this late reply. You can use repository with changesets which comes with built-in support for adding timestamps. There’s an example here.

Hope this helps :slight_smile:


#3

The valid link is now http://rom-rb.org/4.0/learn/core/changesets/