# Locking a post in the console

**URL:** https://meta.discourse.org/t/locking-a-post-in-the-console/106410
**Category:** Development
**Created:** [13.Январь.2019 11:15:14 UTC](https://meta.discourse.org/t/locking-a-post-in-the-console/106410 "2019-01-13T11:15:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![csmu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/csmu/32/124581_2.png) [@csmu](https://meta.discourse.org/u/csmu)
#### Post date: [13.Январь.2019 11:15:15 UTC](https://meta.discourse.org/t/locking-a-post-in-the-console/106410/1 "2019-01-13T11:15:15Z")

</div>

How do you lock a post in the console?

Is it as simple as setting post.locked\_by\_id to a valid user id?

---

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [13.Январь.2019 11:28:35 UTC](https://meta.discourse.org/t/locking-a-post-in-the-console/106410/2 "2019-01-13T11:28:35Z")

</div>

I assume that you refer to closing a topic.

I’d either use the API for that or the corresponding method on the `Topic` type:

```plaintext
Topic.where(closed: false, category_id: <catid>, id: <tid>).find_each do |topic| 
  topic.update_status("closed", true, Discourse.system_user)
end

```

Edit: I just realize that locking posts is something different. You can set the `post.locked_by_id` but I guess you’re not creating a logged staff action in that case.

---

<div class="post-metadata">

### Author: ![csmu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/csmu/32/124581_2.png) [@csmu](https://meta.discourse.org/u/csmu)
#### Post date: [13.Январь.2019 11:33:02 UTC](https://meta.discourse.org/t/locking-a-post-in-the-console/106410/3 "2019-01-13T11:33:02Z")

</div>

Thanks @fhe

That’s useful 🙂

I’m looking how to lock a post at this stage.

Context. A topic that is created in 2015,edited in 2018. An old reply in 2015. The 2015 question was answered and it makes sense to lock it as the question is no longer in context the new version of the topic.

---

<div class="post-metadata">

### Author: ![csmu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/csmu/32/124581_2.png) [@csmu](https://meta.discourse.org/u/csmu)
#### Post date: [13.Январь.2019 11:34:18 UTC](https://meta.discourse.org/t/locking-a-post-in-the-console/106410/4 "2019-01-13T11:34:18Z")

</div>

> [@fhe](#):
>
> I just realize that locking posts is something different. You can set the `post.locked_by_id` but I guess you’re not creating a logged staff action in that case.

Can a logged staff action be created in the console?

---

<div class="post-metadata">

### Author: ![fhe](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fhe/32/113345_2.png) [@fhe](https://meta.discourse.org/u/fhe)
#### Post date: [13.Январь.2019 11:36:58 UTC](https://meta.discourse.org/t/locking-a-post-in-the-console/106410/5 "2019-01-13T11:36:58Z")

</div>

> [@csmu](#):
>
> I’m looking how to lock a post at this stage.

Yes, I’ve just realized that and edited my post accordingly.

> [@csmu](#):
>
> Can a logged staff action be created in the console?

I think utilizing the `PostLocker` from the console would be good, it also creates the staff log action.

```plaintext
Post.where(id: <pid>).find_each do |post| 
  locker = PostLocker.new(post, Discourse.system_user)
  locker.lock
end
```
