Editing post date?


#1

Hi there,

How would I go about altering the post date on a topic or post within a topic?


Changing timestamp of topic replies - Easier way?
(Jeff Atwood) #2

There is no way to do this at the current time… no pun intended.


#3

Poo :frowning:

Does ‘not possible’ also include from the command-line?


(Jeff Atwood) #4

It’s probably possible via Ruby command line or direct SQL, of course. I only meant the web UI.


(Régis Hanol) #5

Here’s how to do it via the command line

# ssh into your server
cd /var/discourse # or /var/docker on pre-V1 install
./launcher enter app
rails c
post = Post.find_by(id: 1234)
post.created_at = DateTime.new(2001,2,3,0,0,42) # year, month, day, hour, minute, seconds 
post.save

#6

Awesome! This seems to have worked, but I’m unclear on what I’m supposed to do or how to safely exit once I’ve altered the date/time.

Looking at my test install, the date for that post hasn’t changed… I’m sure I’m missing something? Thanks for your help!


(cpradio) #7

CTRL + D is how you exit :wink:


(Jens Maier) #8

Or by typing exit or !!!. :grin:


#9

Hm… I manage to exit out. I was doing it properly (but wanted to ask in case I was just being a moron), but get a message saying:

Unable to write to your history file, history not saved

Thanks again for your help and patience guys; Ruby is still pretty new to me but I’m learning to love it.


(cpradio) #10

Yeah, that’s a different thing all together, one that you probably don’t need to worry about. It is simply telling you it didn’t save the commands you entered so when you connect later you could use history to see what commands you ran previously.

If you re-run rails c and type history, you should get a blank list. If you enter a few commands and run it again, you will see a list of the commands you have run up to that point.


#11

Is there something else I need to do to get the changes to show up on the front-end then? The post I edited is still showing the original date. Reboot Discourse, perhaps?


(cpradio) #12

I’m not sure, @zogstrip does he need to rebake the post? Or clear any caches?


#13

@zogstrip @cpradio

Disregard my last question… In looking at the output in red in those screenshots above, I don’t seem to be targeting the post I think I am… The summary text in there is coming from one of the posts that get created when making a new category.

How would I properly determine a post’s ID? This one, for example:


(cpradio) #14

Performing a view source, contains this:
<article class="boxed" id="post_2" data-bindattr-28="28" data-post-id="39" data-bindattr-29="29" data-user-id="1">

Which has data-post-id="39"

I can’t promise that is the correct answer though…


#15

Yessir, that was it! I suppose it helps if I’m actually changing the correct post ID :wink:

Thank you for all your help!

:beers:


(Jens Maier) #16

By the way, you can also find the post by the topic id and post number which are perhaps easier to read from the forum via the share link instead of inspecting source:

Post.find_by(topic_id: 123, post_number: 45)


#17

This is working excellently, though I do have one more question:

How do I force a refresh or update of the last “activity” on the thread listing page? If all of the dates within a thread have a date of May or June, the last activity shouldn’t say 22 minutes ago

Thanks guys :slight_smile:


(Jens Maier) #18

You can see how Discourse does this internally in the PostCreator class:

So in your case, this should do:

t = Topic.find(123)
t.last_posted_at = t.posts.last.created_at
t.bumped_at = t.posts.last.created_at
t.save!

#19

Thanks! That pointed me in the right direction.

I found that I had to also update bumped_at and updated_at (or whatever their proper names are, I’m not looking at the screen right now).


(Kane York) #20

The bumped_at field is what is displayed & sorted by.