Connecting to database with a plugin


(SMHassanAlavi) #1

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 ?


(James Kiesel) #2

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')

(SMHassanAlavi) #3

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?


#4

Start here: