# New columns in directory (/users)

**URL:** https://meta.discourse.org/t/new-columns-in-directory-users/77814
**Category:** Development
**Created:** [January 12, 2018, 7:57am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814 "2018-01-12T07:57:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Anshul\_TM](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Anshul\_TM](https://meta.discourse.org/u/Anshul_TM)
#### Post date: [January 12, 2018, 7:57am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/1 "2018-01-12T07:57:18Z")

</div>

Hello everyone,

I want to add a few columns in the directory items model. ( ‘Solutions’ and ‘Points’ )  
I was able to modify the code for the model and templates directly, but how can I achieve this via a plugin ?

If not possible via plugin, is it possible to use my own github repo ( with these changes ) instead but somehow continue to get updates from Discourse repo.

Thanks

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [January 12, 2018, 8:01am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/2 "2018-01-12T08:01:28Z")

</div>

I’m Not able to understand the exact usecase but definitely adding some columns can be achieved via plugins!

You may start writing a plugin using the official Guidelines and Then use it in order to ensure You get official updates.

Here is Some Help:

> [@Developing Discourse Plugins - Part 1 - Create a basic plugin](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-1/30515):
>
> Building a plugin in Discourse can be really simple, once you learn a couple of quirks. The goal of this post is to create a skeleton plugin and introduce you to the basics. Your development environment Make sure you have a development environment of Discourse running on your computer. I recommend you use [the appropriate setup guide](https://meta.discourse.org/tag/dev-install) and come back when you’re done. plugin.rbtada Use [GitHub - discourse/discourse-plugin-skeleton: Template for Discourse plugins · GitHub](https://github.com/discourse/discourse-plugin-skeleton) to create a complete di…

---

<div class="post-metadata">

### Author: ![Anshul\_TM](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@Anshul\_TM](https://meta.discourse.org/u/Anshul_TM)
#### Post date: [January 12, 2018, 8:17am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/3 "2018-01-12T08:17:19Z")

</div>

Hey @itsbhanusharma, thanks for the link.  
I’ve gone through the Beginner’s Guide, but couldn’t find DB modifications in it.

You’ve any plugin in mind which does something similar ?

---

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [January 12, 2018, 8:34am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/4 "2018-01-12T08:34:53Z")

</div>

Have You gone through the whole series?

I’d have a look in the Plugin repo to find if there is something.

EDIT: have a Look Here maybe this Helps!

> [@Create custom db table and fields from plugin](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164):
>
> Hi everyone, I’m working on developing a new plugin and I need to store some values in the database but I do not want to modify the discourse base system in any fashion. I’ve read through the beginners guide on plugin development and I did not see anything addressing this, essentially what i need is a table that has two fields that reference a single user that links another user or multiple users. database → plugin\_users → column 1 would be user\_id and column 2 could be a comma separated list o…

---

<div class="post-metadata">

### Author: ![gdpelican](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gdpelican/32/81308_2.png) [@gdpelican](https://meta.discourse.org/u/gdpelican)
#### Post date: [August 8, 2018, 5:40am UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/5 "2018-08-08T05:40:55Z")

</div>

FWIW I’ve decided to go this route with a plugin of mine, where PluginStoreRow and CustomFields didn’t qqquite cut it on their own. ⚠ **Caveat emptor** ⚠ You should be trying really hard not to do this - It’s not recommended behaviour to modify the database from within a plugin, but it can be done unobtrusively in small, additive doses (I’d say you _never for any reason_ want to remove or change existing columns)

Here’s how I did it:

- Write a migration in the plugin which modifies the DB

```plaintext
# plugins/my_plugin/migrations/add_column.rb
class AddColumn < ActiveRecord::Migration[5.1]
  def change
    add_column :post_custom_fields, :my_column, :jsonb, default: {}, index: true
  end
end

```

_NB: it’s a good idea to namespace your column with the name of your plugin, since adding a column with a generic name like ‘value’ could end up actually existing in the future and then you’d have a **real** problem._

- When the plugin is loaded, check to see if the migration has been run, and if not, run it

```plaintext
# plugin.rb
if !PostCustomField.new.respond_to?(:my_column)
  require Rails.root.join('plugins', 'my_plugin', 'migrations', 'add_column')
  AddColumns.new.up # <-- this runs the migration
end

```

- The first time you run `rails s` or `rails c` after this, you should see the following:

```plaintext
add_column(:post_custom_fields, :my_column, :jsonb, {:default=>{}, :index=>true})

```

that means it worked! Subsequent boots of the app shouldn’t display this message.

- **Important, don’t skip plz** - Provide a method for users to clean up your database changes if they don’t want your plugin anymore. I did it with a rake task.

```plaintext
# plugins/my_plugin/tasks/remove_column.rake

# (I think there's a better way to do this, but this line works.)
# Essentially, we need the environment loaded so that ActionRecord::Migration is defined.
require File.expand_path(File.dirname( __FILE__ ) + '../../../config/environment'

desc "remove my_column from the database"
task "my_column:remove" do
  require Rails.root.join('plugins', 'my_plugin', 'migrations', 'add_column')
  AddColumn.new.down
end

```

Then, in your README, instructions on how to use it

```plaintext
This plugin adds a column to the database. If you want to remove this plugin and don't plan on using it again, perform the following steps:

- Enter your discourse container 
./launcher enter app
- Run the following command to reverse the database changes
bundle exec rake -f plugins/my_plugin/tasks/remove_column.rake -T
- Remove the 'git clone' line from your app.yml
- Rebuild your container
./launcher rebuild app

Note that dropping this column is permanent and irreversible for the data stored there..

```

As always, let me know if I’ve missed something or if this can be improved.

---

<div class="post-metadata">

### Author: ![spirobel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spirobel/32/170908_2.png) [@spirobel](https://meta.discourse.org/u/spirobel)
#### Post date: [July 8, 2019, 6:55pm UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/6 "2019-07-08T18:55:18Z")

</div>

I couldnt find this active calling of the up method in the rails active record docs.

> ```
> AddColumns.new.up  
> 
> ```

it seems like migrating is usually done by rake db:migrate commands and not directly with ruby.  
so it seems to me that its better to have a plugin migrate dir and then make symlinks from it to the /discourse/db/migrate directory in the plugin.rb file. this way the rails infrastructure for migration can also be used with rollback. maybe add documentation to roll back the specific changes to the plugin readme. seems like its possible with something like this to rollback specific migrations:

> rake db:migrate:down VERSION=20100905201547

[https://stackoverflow.com/questions/3647685/how-to-rollback-a-specific-migration](https://stackoverflow.com/questions/3647685/how-to-rollback-a-specific-migration)  
I would be glad to get some comments on this. I also think that its not productive to just link to the custom fields. I want to use discourse to create a chinese-english dictionary. I cant rely on only custom fields for this.

---

<div class="post-metadata">

### Author: ![spirobel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spirobel/32/170908_2.png) [@spirobel](https://meta.discourse.org/u/spirobel)
#### Post date: [October 16, 2019, 8:34pm UTC](https://meta.discourse.org/t/new-columns-in-directory-users/77814/7 "2019-10-16T20:34:08Z")

</div>

I created a migration and model generator for plugins: [GitHub - spirobel/discourse at plugin\_model\_and\_migrations · GitHub](https://github.com/spirobel/discourse/tree/plugin_model_and_migrations)  
I think the way to handle plugin migrations like it is shown here is not a good idea.
