Set manually the created_at and updated_at attributes on object creation

432 views
Skip to first unread message

Cassio Almeida

unread,
Sep 25, 2017, 1:22:12 AM9/25/17
to Ruby on Rails: Talk
Hey guys, how do I manually update created_at and updated_at attributes at the time of object creation, I already know that this does not work:

post = Post.new(
  title: "Hello",
  body: "Hello World!",
  created_at: mycreatedate, 
  updated_at: mychangedate
)

post.save

Colin Law

unread,
Sep 25, 2017, 2:14:12 AM9/25/17
to Ruby on Rails: Talk


On 25 Sep 2017 6:21 a.m., "Cassio Almeida" <jcassi...@gmail.com> wrote:
Hey guys, how do I manually update created_at and updated_at attributes at the time of object creation

I suggest not doing that. If you want to save datetimes that are not the same as created and updated then add new attributes to do what you want. 

Colin



, I already know that this does not work:

post = Post.new(
  title: "Hello",
  body: "Hello World!",
  created_at: mycreatedate, 
  updated_at: mychangedate
)

post.save

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ee93e41b-8bfd-40bc-9bb5-60811e7be844%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Edwards

unread,
Sep 29, 2017, 11:31:00 AM9/29/17
to Ruby on Rails: Talk
And rightly so, it violates basic activerecord principles and may well break stuff. 
Reply all
Reply to author
Forward
0 new messages