# How do you set timestamps in version 4.0?

**URL:** https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186
**Category:** Site Feedback
**Created:** [October 21, 2017, 9:55pm UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186 "2017-10-21T21:55:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gus.black](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/gus.black/32/110_2.png) [@gus.black](https://discourse.rom-rb.org/u/gus.black)
#### Post date: [October 21, 2017, 9:55pm UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/1 "2017-10-21T21:55:13Z")

</div>

The way we were setting timestamps is no longer working.

┆ ┆ repo.changeset(attrs).map(:add\_timestamps)

The example here seems to be out of date: [http://rom-rb.org/learn/repositories/changesets/](http://rom-rb.org/learn/repositories/changesets/)

Because repo no longer has the changeset method. Are there any examples on how to do it in the new version?

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)
#### Post date: [October 21, 2017, 10:23pm UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/2 "2017-10-21T22:23:41Z")

</div>

Changesets are now available directly on relations. See [Core / Changeset](http://rom-rb.org/4.0/learn/core/changesets/) docs.

---

<div class="post-metadata">

### Author: ![gus.black](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/gus.black/32/110_2.png) [@gus.black](https://discourse.rom-rb.org/u/gus.black)
#### Post date: [October 21, 2017, 10:46pm UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/3 "2017-10-21T22:46:13Z")

</div>

@solnic Apologies, this might just be a newbie mistake. But when I try to access the changeset method through the relations I still get the ‘undefined method `changeset’ error. Am I missing something?

```
 [6] pry(IndieNinja::API)> users_repo.users

=> #<IndieNinja::API::Persistence::Relations::Users name=ROM::Relation::Name(users) dataset=#<Sequel::Postgres::Dataset: "SELECT \"users\".\"id\", \"users\".\"email\", \"users\".\"first_name\", \"users\".\"last_name\", \"users\".\"created_at\", \"users\".\"updated_at\", \"users\".\"identity_provider_id\" FROM \"users\" ORDER BY \"users\".\"id\"">>

[7] pry(IndieNinja::API)> users_repo.users.changeset
NoMethodError: undefined method `changeset' for #<IndieNinja::API::Persistence::Relations::Users:0x007fb958a320a0>
from (pry):6:in `<module:API>'
```

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)
#### Post date: [October 22, 2017, 8:29am UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/4 "2017-10-22T08:29:24Z")

</div>

Do you have `rom` gem as a dependency and `require` it? It should require `rom-changeset` which adds `Relation#changeset` method.

---

<div class="post-metadata">

### Author: ![gus.black](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/gus.black/32/110_2.png) [@gus.black](https://discourse.rom-rb.org/u/gus.black)
#### Post date: [October 22, 2017, 11:10am UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/5 "2017-10-22T11:10:33Z")

</div>

@solnic That was the issue!! I only had rom-repository and rom-sql in my Gemfile!

Thank you.

---

<div class="post-metadata">

### Author: ![gus.black](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/gus.black/32/110_2.png) [@gus.black](https://discourse.rom-rb.org/u/gus.black)
#### Post date: [October 22, 2017, 4:14pm UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/6 "2017-10-22T16:14:44Z")

</div>

For anyone coming across this here is a working example on how we got timestamps working:

We have a timestamps module which we include in our repos and it looks like this:

```
  module Persistence::Timestamps
  ┆ def create(attrs)
  ┆ ┆ super root.changeset(:create, attrs).map(:add_timestamps)
    end

  ┆ def update(attrs)
  ┆ ┆ super root.changeset(:update, attrs).map(:add_timestamps)
  ┆ end
  end
```

---

<div class="post-metadata">

### Author: ![buszu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/buszu/32/132_2.png) [@buszu](https://discourse.rom-rb.org/u/buszu)
#### Post date: [February 28, 2018, 7:36pm UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/7 "2018-02-28T19:36:28Z")

</div>

I have also problem with setting timestamps. My repo looks like:

```ruby
require_relative './repository_base'

class DocumentationRepository < ROM::Repository[:documentations]
  include RepositoryBase
  # ...
end

```

And repository base is:

```ruby
module RepositoryBase
  def self.included(base)
    base.class_eval do
      struct_namespace Entities
      commands :create, update: :by_pk, delete: :by_pk
    end
  end

  def create(attrs)
    super root.changeset(:create, attrs).map(:add_timestamps)
  end

  def update(attrs)
    super root.changeset(:update, attrs).map(:add_timestamps)
  end
end

```

However create and update methods don’t seem to be reloaded. Still getting `ROM::SQL::NotNullConstraintError` for timestamps.

Edit - quoting @flash\_gordon:  
@buszu that’s because the commands macro overrides your create/update methods in the target class. If you’re using changesets just do

`def create(attrs); root.changeset(:create, attrs).map(:add_timestamps).commit`

and don’t use commands for update it’s similar,

`def update(id:, **attrs); root.by_pk(id).changeset(:update, attrs).extend(:touch).commit`

---

<div class="post-metadata">

### Author: ![smaximov](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/smaximov/32/109_2.png) [@smaximov](https://discourse.rom-rb.org/u/smaximov)
#### Post date: [April 2, 2018, 12:24pm UTC](https://discourse.rom-rb.org/t/how-do-you-set-timestamps-in-version-4-0/186/8 "2018-04-02T12:24:09Z")

</div>

> [@buszu](#):
>
> However create and update methods don’t seem to be reloaded

Don’t you need to prepend `RepositoryBase` instead of including it so `create`/`update` would actually override base methods?
