Connecting to database with a plugin

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 ?

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

User.register_custom_field_type('unique_number', :integer)

which you can store before save:

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:

user.get('custom_fields.unique_number')
8 Likes

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?

Start here:

2 Likes