API topic post creation ordered by updated_at or created_at?

I am creating a lot of topics with posts from an older system. The topics are from a few years ago and the posts within the topics the same.
What I am finding is that the posts associated with the topic show the correct time of created_at but seem to order by updated_at.
updated_at is the date and time that I created the post within my script which is not necessarily the order that they should be displayed.
I am using the discourse_api gem for all the Discourse work.

Is there a way to solve this? Or am I seeing this wrong? Or is there a setting that I can chage to display them in the correct order?

Thanks
Michael

Post order isn’t something that can be changed. Even if a post was updated later it shouldn’t change the post order.

Can you share an example of the code you are using to create topics and posts?

2 Likes

Hi Blake,

So this is what I used to add the posts into the topic

discourse.create_post(
    topic_id: discourse_topic['topic_id'],
    raw: canvas_post_message,
    created_at: canvas_post['created_at']
  )

it goes in fine and the posts are accociated with the topic
if I look at the order then they are not in the order of created_at but something else.

See JSON example of a topic below

{"post_stream":{"posts":[{"id":20907,"name":"Michael Smith","username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png","created_at":"2014-09-16T04:19:47.000Z","cooked":"\u003cp\u003e\u003cstrong\u003eAshley Morrison\u003c/strong\u003e said:\u003c/p\u003e\n\u003cp\u003eGreetings all,\u003c/p\u003e\n\u003cp\u003eI am looking to see if anyone out there knows of any good software to be used with checking and logging faults with Plant and Equipment (Not IT).  Our Tech Studies has the need, under WH\u0026amp;S Rules, to keep records of inspections and work done on the equipment.  Currently they use an access db for the equipment list, and a paper record of the inspections.  We have tried to put this into the DB, but being and in-house design, it just didn’t work.\u003c/p\u003e\n\u003cp\u003eDoes anyone out there know of some software that suits this need.  If it is server based with audit ability so we can see when/who did the inspections this would be great.\u003c/p\u003e\n\u003cp\u003eLooking forward to hearing about lots of great thus.\u003c/p\u003e\n\u003cp\u003eThanks in advance.\u003c/p\u003e\n\u003cp\u003eAsh\u003cbr\u003e\nSt Michael’s College, Adelaide\u003c/p\u003e","post_number":1,"post_type":1,"updated_at":"2018-04-03T22:21:01.603Z","reply_count":0,"reply_to_post_number":null,"quote_count":0,"avg_time":null,"incoming_link_count":0,"reads":1,"score":0,"yours":true,"topic_id":4044,"topic_slug":"plant-and-equipment-registars","display_username":"Michael Smith","primary_group_name":null,"primary_group_flair_url":null,"primary_group_flair_bg_color":null,"primary_group_flair_color":null,"version":1,"can_edit":true,"can_delete":false,"can_recover":null,"can_wiki":true,"read":true,"user_title":null,"actions_summary":[{"id":3,"can_act":true},{"id":4,"can_act":true},{"id":8,"can_act":true},{"id":7,"can_act":true}],"moderator":false,"admin":true,"staff":true,"user_id":3,"hidden":false,"hidden_reason_id":null,"trust_level":1,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false,"user_created_at":"2018-03-25T11:07:37.725Z","user_date_of_birth":null,"can_accept_answer":false,"can_unaccept_answer":false,"accepted_answer":false},{"id":20908,"name":"Michael Smith","username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png","created_at":"2014-09-17T05:51:20.000Z","cooked":"\u003cp\u003e** Matthew Mitchell** said:\u003c/p\u003e\n\u003cp\u003eHi Ash, \u003c/p\u003e\n\u003cp\u003eYou could try local company hardcat, they worked on a similar project with Adelaide Shores - \u003ca href=\"http://www.hardcat.com/adelaide-shores/\"\u003ehttp://www.hardcat.com/adelaide-shores/\u003c/a\u003e - its a bigger scale, however they may be able to assist with you with your needs also.\u003c/p\u003e\n\u003cp\u003eAll the best,\u003c/p\u003e\n\u003cp\u003eMatt\u003c/p\u003e\n\u003cp\u003e \u003c/p\u003e","post_number":2,"post_type":1,"updated_at":"2018-04-03T22:21:02.986Z","reply_count":0,"reply_to_post_number":null,"quote_count":0,"avg_time":null,"incoming_link_count":0,"reads":1,"score":0,"yours":true,"topic_id":4044,"topic_slug":"plant-and-equipment-registars","display_username":"Michael Smith","primary_group_name":null,"primary_group_flair_url":null,"primary_group_flair_bg_color":null,"primary_group_flair_color":null,"version":1,"can_edit":true,"can_delete":true,"can_recover":null,"can_wiki":true,"link_counts":[{"url":"http://www.hardcat.com/adelaide-shores/","internal":false,"reflection":false,"clicks":0}],"read":true,"user_title":null,"actions_summary":[{"id":3,"can_act":true},{"id":4,"can_act":true},{"id":8,"can_act":true},{"id":7,"can_act":true}],"moderator":false,"admin":true,"staff":true,"user_id":3,"hidden":false,"hidden_reason_id":null,"trust_level":1,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false,"user_created_at":"2018-03-25T11:07:37.725Z","user_date_of_birth":null,"can_accept_answer":false,"can_unaccept_answer":false,"accepted_answer":false},{"id":20909,"name":"Michael Smith","username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png","created_at":"2014-09-16T05:10:58.000Z","cooked":"\u003cp\u003e\u003cstrong\u003eMatt Simpson\u003c/strong\u003e said:\u003c/p\u003e\n\u003cp\u003eHi Ash,\u003c/p\u003e\n\u003cp\u003eWe’ve had great success for the past few years with a custom-built Zoho database for a similar purpose (\u003ca href=\"http://creator.zoho.com\"\u003ecreator.zoho.com\u003c/a\u003e). There are similarities to Access/Filemaker: it’s a drag and drop database builder, though it has a powerful back end if you want to code trickery into the design.  \u003c/p\u003e\n\u003cp\u003eAs an example, our Help Desk uses it to keep track of repairs. It is easy to enter jobs, see the status, follow up, write notes, automatically pull down serial numbers etc, plus it does some other helpful things like email users updates, remind users to bring back borrowed items etc.\u003c/p\u003e\n\u003cp\u003eIf you have someone moderately technical to put it together, it’s great. You can design your DB to store exactly what you need and nothing you don’t, in about an hour.\u003c/p\u003e\n\u003cp\u003eOtherwise a system like OTRS (as Ross said) or WebHelpDesk might be what you need, sadly with the associated extra complexity.\u003c/p\u003e\n\u003cp\u003eHope that helps,\u003cbr\u003e\nMatt\u003c/p\u003e","post_number":3,"post_type":1,"updated_at":"2018-04-03T22:21:03.275Z","reply_count":0,"reply_to_post_number":null,"quote_count":0,"avg_time":null,"incoming_link_count":0,"reads":1,"score":0,"yours":true,"topic_id":4044,"topic_slug":"plant-and-equipment-registars","display_username":"Michael Smith","primary_group_name":null,"primary_group_flair_url":null,"primary_group_flair_bg_color":null,"primary_group_flair_color":null,"version":1,"can_edit":true,"can_delete":true,"can_recover":null,"can_wiki":true,"link_counts":[{"url":"http://creator.zoho.com","internal":false,"reflection":false,"clicks":0}],"read":true,"user_title":null,"actions_summary":[{"id":3,"can_act":true},{"id":4,"can_act":true},{"id":8,"can_act":true},{"id":7,"can_act":true}],"moderator":false,"admin":true,"staff":true,"user_id":3,"hidden":false,"hidden_reason_id":null,"trust_level":1,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false,"user_created_at":"2018-03-25T11:07:37.725Z","user_date_of_birth":null,"can_accept_answer":false,"can_unaccept_answer":false,"accepted_answer":false},{"id":20910,"name":"Michael Smith","username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png","created_at":"2014-09-16T05:52:14.000Z","cooked":"\u003cp\u003e\u003cstrong\u003eAshley Morrison\u003c/strong\u003e said:\u003c/p\u003e\n\u003cp\u003eMatt,\u003c/p\u003e\n\u003cp\u003eThanks for that.  Yes we use WebHelpDesk now but that doesn’t suit the need here.  This is more a Maintenance repair audit style of things, than just Job Tracking (something WHD does do very well and we love it for it).\u003c/p\u003e\n\u003cp\u003e \u003c/p\u003e\n\u003cp\u003eAsh.\u003c/p\u003e","post_number":4,"post_type":1,"updated_at":"2018-04-03T22:21:03.546Z","reply_count":0,"reply_to_post_number":null,"quote_count":0,"avg_time":null,"incoming_link_count":0,"reads":1,"score":0,"yours":true,"topic_id":4044,"topic_slug":"plant-and-equipment-registars","display_username":"Michael Smith","primary_group_name":null,"primary_group_flair_url":null,"primary_group_flair_bg_color":null,"primary_group_flair_color":null,"version":1,"can_edit":true,"can_delete":true,"can_recover":null,"can_wiki":true,"read":true,"user_title":null,"actions_summary":[{"id":3,"can_act":true},{"id":4,"can_act":true},{"id":8,"can_act":true},{"id":7,"can_act":true}],"moderator":false,"admin":true,"staff":true,"user_id":3,"hidden":false,"hidden_reason_id":null,"trust_level":1,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false,"user_created_at":"2018-03-25T11:07:37.725Z","user_date_of_birth":null,"can_accept_answer":false,"can_unaccept_answer":false,"accepted_answer":false},{"id":20911,"name":"Michael Smith","username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png","created_at":"2014-09-16T04:53:06.000Z","cooked":"\u003cp\u003e\u003cstrong\u003eRoss Gailer\u003c/strong\u003e said:\u003c/p\u003e\n\u003cp\u003eYou could possibly leverage one of the free helpdesk systems, such as OTRS. You would be able to track assets, inspections could be entered as tickets with an appropriate template, and you would have full audit on who had entered the information etc. Server based with a MySQL back end and a web front end they could run on iPads, smart phones, etc if they have WiFi coverage where they are doing the job.\u003c/p\u003e","post_number":5,"post_type":1,"updated_at":"2018-04-03T22:21:03.829Z","reply_count":0,"reply_to_post_number":null,"quote_count":0,"avg_time":null,"incoming_link_count":0,"reads":1,"score":0,"yours":true,"topic_id":4044,"topic_slug":"plant-and-equipment-registars","display_username":"Michael Smith","primary_group_name":null,"primary_group_flair_url":null,"primary_group_flair_bg_color":null,"primary_group_flair_color":null,"version":1,"can_edit":true,"can_delete":true,"can_recover":null,"can_wiki":true,"read":true,"user_title":null,"actions_summary":[{"id":3,"can_act":true},{"id":4,"can_act":true},{"id":8,"can_act":true},{"id":7,"can_act":true}],"moderator":false,"admin":true,"staff":true,"user_id":3,"hidden":false,"hidden_reason_id":null,"trust_level":1,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false,"user_created_at":"2018-03-25T11:07:37.725Z","user_date_of_birth":null,"can_accept_answer":false,"can_unaccept_answer":false,"accepted_answer":false},{"id":20912,"name":"Michael Smith","username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png","created_at":"2018-04-03T22:21:04.065Z","cooked":"","post_number":6,"post_type":3,"updated_at":"2018-04-03T22:21:04.065Z","reply_count":0,"reply_to_post_number":null,"quote_count":0,"avg_time":null,"incoming_link_count":0,"reads":1,"score":0,"yours":true,"topic_id":4044,"topic_slug":"plant-and-equipment-registars","display_username":"Michael Smith","primary_group_name":null,"primary_group_flair_url":null,"primary_group_flair_bg_color":null,"primary_group_flair_color":null,"version":1,"can_edit":true,"can_delete":true,"can_recover":null,"can_wiki":true,"read":true,"user_title":null,"actions_summary":[{"id":3,"can_act":true},{"id":4,"can_act":true},{"id":8,"can_act":true},{"id":7,"can_act":true}],"moderator":false,"admin":true,"staff":true,"user_id":3,"hidden":false,"hidden_reason_id":null,"trust_level":1,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false,"action_code":"archived.enabled","user_created_at":"2018-03-25T11:07:37.725Z","user_date_of_birth":null,"can_accept_answer":false,"can_unaccept_answer":false,"accepted_answer":false}],"stream":[20907,20908,20909,20910,20911,20912]},"timeline_lookup":[[1,1296],[2,1295],[3,1296],[6,1]],"suggested_topics":[{"id":8,"title":"Welcome to Discourse","fancy_title":"Welcome to Discourse","slug":"welcome-to-discourse","posts_count":1,"reply_count":0,"highest_post_number":1,"image_url":"//timemachine.mitie.edu.au/images/welcome/discourse-edit-post-animated.gif","created_at":"2018-03-25T00:52:35.009Z","last_posted_at":"2018-03-25T00:52:35.044Z","bumped":true,"bumped_at":"2018-03-25T00:59:01.561Z","unseen":false,"pinned":true,"unpinned":null,"excerpt":"Tada! \nThe first paragraph of this pinned topic will be visible as a welcome message to all new visitors on your homepage. It’s important! \nEdit this into a brief description of your community: \n\nWho is it for?\nWhat can \u0026hellip;","visible":true,"closed":false,"archived":false,"bookmarked":null,"liked":null,"archetype":"regular","like_count":0,"views":1,"category_id":1,"featured_link":null,"posters":[{"extras":"latest single","description":"Original Poster, Most Recent Poster","user":{"id":-1,"username":"system","avatar_template":"/user_avatar/timemachine.mitie.edu.au/system/{size}/2_1.png"}}]}],"id":4044,"title":"Plant and Equipment Registars","fancy_title":"Plant and Equipment Registars","posts_count":6,"created_at":"2014-09-16T04:19:47.000Z","views":1,"reply_count":0,"like_count":0,"last_posted_at":"2018-04-03T22:21:04.065Z","visible":true,"closed":false,"archived":true,"has_summary":false,"archetype":"regular","slug":"plant-and-equipment-registars","category_id":5,"word_count":498,"deleted_at":null,"pending_posts_count":0,"user_id":3,"pm_with_non_human_user":false,"featured_link":null,"pinned_globally":false,"pinned_at":null,"pinned_until":null,"draft":null,"draft_key":"topic_4044","draft_sequence":6,"posted":true,"unpinned":null,"pinned":false,"details":{"created_by":{"id":3,"username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png"},"last_poster":{"id":3,"username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png"},"participants":[{"id":3,"username":"michael.smith","avatar_template":"/letter_avatar_proxy/v2/letter/m/eada6e/{size}.png","post_count":6,"primary_group_name":null,"primary_group_flair_url":null,"primary_group_flair_color":null,"primary_group_flair_bg_color":null}],"links":[{"url":"http://creator.zoho.com","title":null,"fancy_title":null,"internal":false,"attachment":false,"reflection":false,"clicks":0,"user_id":3,"domain":"creator.zoho.com","root_domain":"zoho.com"},{"url":"http://www.hardcat.com/adelaide-shores/","title":null,"fancy_title":null,"internal":false,"attachment":false,"reflection":false,"clicks":0,"user_id":3,"domain":"www.hardcat.com","root_domain":"hardcat.com"}],"notification_level":2,"notifications_reason_id":4,"can_move_posts":true,"can_edit":true,"can_delete":true,"can_remove_allowed_users":true,"can_remove_self_id":3,"can_invite_to":true,"can_create_post":true,"can_reply_as_new_topic":true,"can_flag_topic":true,"can_convert_topic":true},"current_post_number":1,"highest_post_number":6,"last_read_post_number":6,"last_read_post_id":20912,"deleted_by":null,"has_deleted":false,"actions_summary":[{"id":4,"count":0,"hidden":false,"can_act":true},{"id":8,"count":0,"hidden":false,"can_act":true},{"id":7,"count":0,"hidden":false,"can_act":true}],"chunk_size":20,"bookmarked":false,"topic_timer":null,"private_topic_timer":null,"message_bus_last_id":6,"participant_count":1,"can_vote":false,"vote_count":null,"user_voted":false}

In this you can see that the created_at is not in order and it seems to be updated_at that is being used to order.

Am I seeing that right or is something else going on?

Cheers
Michael

And if you take a screen shot of that topic are the posts out of order?

So yes it is possible to create posts out of order using the API. See my example below where I added a post from 2012 after a post from 2013:

But this is only possible via the API and the solution is to sort your posts first before using the API to create them so that they show up in the correct order.

I believe posts are sorted by id and not by created_at or updated_at.

4 Likes

okay makes sense. Thanks for checking.
I will test out ordering them and then add them through the API.

1 Like

ordering the posts before adding via API has worked. Thanks @blake for your help.

4 Likes

Just wanted to update here in case someone has a lot of posts out of order due to an import or some other reason that there is now a rake task to sort posts by date created.

https://github.com/discourse/discourse/commit/cf9965fd543543b4d1162adb76f157812f6fbcf8

6 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.