User page with custom data from external DB

I’ve setup all routes and everything is working correct but now I’m at the part where I actually have to hook up the routes with my custom data and I’m not sure how to connect with the MySQL database.
I’ve created settings for the host/username/password etc and now I wanna create a database connection.
When I google it I only get results that you have to create a database config file but I don’t really get this working because I assume that it doesn’t work for plugins?
I did find some Ruby tutorials for MySQL databases but I’m not sure if this is what I’m looking for.

I tried this to get the connection but I don’t think it’s the correct way and it’s also giving me errors.

[details=Summary]I put the following code in plugin.rb

  ActiveRecord::Base.establish_connection(
  :adapter  => 'mysql',
  :database => SiteSetting.curvefever_mysql_database,
  :host     => SiteSetting.curvefever_mysql_host,
  :username => SiteSetting.curvefever_mysql_username,
  :password => SiteSetting.curvefever_mysql_password
  )

It gives me this error.
Specified 'mysql' for database adapter, but the gem is not loaded. Add `gem 'mysql'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
The error is quite obvious but is it possible to add them gem in a plugin?[/details]

EDIT: So I’ve found out I can load a gem in the plugin but I’m unable to get it installed.
I managed to install the gem with ruby locally but when I put it in my plugin it won’t install.
ERROR: Error installing mysql2: ERROR: Failed to build gem native extension.
When I google the error It tells me to install mysql and ruby etc and I did all that and now it lets me install it with gem install mysql2 but this still doesn’t fix the install for the plugin.
I just put gem 'mysql2', '0.4.5' in my plugin.rb.

full log file: https://gist.github.com/Rojoss/367c8e473317d0b7a65845c00ef7a9f4

It seems kinda weird that I have to install MySQL and stuff locally if I wanna connect to an external database.
When I load the plugin on my forum in production it also needs to work without having to install a bunch of stuff.


Another question, once I have the database hooked up and I’m able to fetch custom user data how does caching work?
I found some guides on making API’s in rails apps but will this work as discourse plugin in the controllers?
The user data is pretty dynamic but I don’t want users to be able to spam refresh and send a ton of queries.
Something like 5 minute caching for user data would be great.
Would I have to write my own caching for this or is there something I can use in rails?

In general I’d like to know to what extend I can follow up guides online for rails apps will everything just work fine in a plugin? Are there restrictions and things that can’t be done in plugins?

2 Likes