# Connecting to database with a plugin

**URL:** https://meta.discourse.org/t/connecting-to-database-with-a-plugin/52300
**Category:** Development
**Created:** [November 1, 2016, 1:44pm UTC](https://meta.discourse.org/t/connecting-to-database-with-a-plugin/52300 "2016-11-01T13:44:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [November 1, 2016, 1:44pm UTC](https://meta.discourse.org/t/connecting-to-database-with-a-plugin/52300/1 "2016-11-01T13:44:03Z")

</div>

Hi. I want to write a plugin with mix of javascript and ruby and I want to connect my plugin to database to find an unique number(of if doesn’t exist create a unique number) for each user and I want to use that number in my javascript code.my javascript code sholud send that number as get method in ajax.how should I access that number in javascript or create it ?

---

<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: [November 1, 2016, 2:23pm UTC](https://meta.discourse.org/t/connecting-to-database-with-a-plugin/52300/2 "2016-11-01T14:23:00Z")

</div>

I think you’re looking for custom fields on the user:

```plaintext
User.register_custom_field_type('unique_number', :integer)

```

which you can store before save:

```plaintext
class ::User
  before_save :store_unique_number
  def store_unique_number
    self.custom_fields['unique_number'] ||= rand(1000)
    # ^^ ps do something here to ensure the number is actually unique    
  end
end

```

which will be serialized out and can be accessed on the front end:

```plaintext
user.get('custom_fields.unique_number')

```

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [November 2, 2016, 10:07am UTC](https://meta.discourse.org/t/connecting-to-database-with-a-plugin/52300/3 "2016-11-02T10:07:54Z")

</div>

the problem is I don’t know how to add ruby code to my pluggin .actuly I don’t khow how to write ruby pluggin .is there any refrence for learning?

---

<div class="post-metadata">

### Author: ![Silvanus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/silvanus/32/62831_2.png) [@Silvanus](https://meta.discourse.org/u/Silvanus)
#### Post date: [November 2, 2016, 11:54am UTC](https://meta.discourse.org/t/connecting-to-database-with-a-plugin/52300/4 "2016-11-02T11:54:11Z")

</div>

Start here:

> [@Developing Discourse Plugins - Part 1 - Create a basic plugin](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins/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…
