# 在控制台中关闭主题

**URL:** https://meta.discourse.org/t/closing-a-topic-in-console/107482
**Category:** Development
**Created:** [2019年一月25日 00:32 UTC](https://meta.discourse.org/t/closing-a-topic-in-console/107482 "2019-01-25T00:32:12Z")
**Posts on this page:** 3
**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: [2019年一月25日 00:32 UTC](https://meta.discourse.org/t/closing-a-topic-in-console/107482/1 "2019-01-25T00:32:12Z")

</div>

How do you close a topic in the Rails console?

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [2019年一月25日 02:33 UTC](https://meta.discourse.org/t/closing-a-topic-in-console/107482/2 "2019-01-25T02:33:49Z")

</div>

This should be what you are looking for:

> <https://github.com/discourse/discourse_api/blob/cd7742b9cfc4da2e897004fe328b7e562eac6a46/examples/change_topic_status.rb#L23-L33>

Edit: I read your post too quickly, you were looking for a rails console option. here’s an example you should be able to build off of:

> [@Auto-close old topics from a migrated forum](https://meta.discourse.org/t/auto-close-old-topics-from-a-migrated-forum/25448/10):
>
> I would SSH into the server and then cd /var/discourse ./launcher enter app rails c Topic.where(closed: false).where("created\_at \< '2015-01-01'").find\_each do |topic| topic.update\_status("closed", true, Discourse.system\_user) end And you should have all the topics created before January 1st 2015 closed wink

---

<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: [2019年一月31日 05:05 UTC](https://meta.discourse.org/t/closing-a-topic-in-console/107482/3 "2019-01-31T05:05:31Z")

</div>

Thanks

> [@tshenry](#):
>
> I read your post too quickly, you were looking for a rails console option. here’s an example you should be able to build off of:

Here’s the function I wrote.

```plaintext
	def close_sabai_topics(path)
    
	   if File.exists?(path) then
     
	     import_id_user_id_hash = UserCustomField.where(name: 'import_id').pluck(:value, :user_id).to_h
	     import_id_post_id_hash = PostCustomField.where(name: 'import_id').pluck(:value, :post_id).to_h
     
	     post_id_topic_id_hash = Post.all.pluck(:id,:topic_id).to_h
     
	     posts = Post.all
	     posts_hash = Hash.new
	     posts.each do |post|
	         posts_hash[post.id] = post
	     end
     
	     topics = Topic.all
	     topics_hash = Hash.new
	     topics.each do |topic|
	       topics_hash[topic.id] = topic
	     end
     
	     json = JSON.load File.new(path)     
	     data = json['data']
     
	     data.each do |closed|
	       imported_user_id = "#{closed['user_id']}"
	       imported_post_id = "sabai:#{closed['post_id']}"
	       closed_at = closed['closed_at']
	       user_id = import_id_user_id_hash[imported_user_id]
	       if user_id != nil then
	         post_id = import_id_post_id_hash[imported_post_id]
	         puts "imported_post_id: #{imported_post_id} post_id: #{post_id}"
	         if post_id != nil then
	           post = posts_hash[post_id]
	           if post != nil then
	             topic = topics_hash[post.topic_id]
	             if topic != nil then
	               puts "#{topic.slug}"
	               topic.update_status("closed", true, Discourse.system_user)
	             else
	                raise "Unexpected issue with finding topic.id: #{post.topic_id}"
	             end
	           else
	               # ignore 
	               # puts "Unexpected issue with finding post.id: #{post_id}"
	           end # if post != nil then
	         else
	           # ignore return import_id_post_id_hash
	           # puts "Unexpected issue with finding post_custom_field.value: #{imported_post_id}"
	         end # if post_id != nil then         
	       else
	         raise "Unexpected issue with finding user_custom_field.value: #{imported_user_id}"
	       end # if user_id != nil then
	     end # data.each do |closed|
	   else
	     raise "Unexpected issue with finding path: #{path}"
	   end # if File.exists?(path) then
   
	end

	error = close_sabai_topics('/shared/sabai/questions-closed.json')

```
