Can I have a thread-safe configuration for each query to connect to the database and table_name in ActiveRecord (or Mongoid)?

Also known as the question <"The user has many databases."

Wednesday

My application looks like this:

user has_many databases  
database has_many tables  
table has_many rows  
row habtm(+value) columns   

you get the idea!

Therefore, instead of modeling the database inside the database, I would like to:

  • The sqlite3 database in which users are stored, and
  • many sqlite databases for each user

Each user will LCRUD his tables in their databases (similar to phpmyadmin)

Problem

I would like to have a thread-safe configuration for each request database connection and table_name

class Table < ActiveRecord::Base
end

# in some controller
# set the connection to the user-selected database from some DB list
Table.connection = current_user.session.connection
# set the name to the user-selected table from some tables list
Table.table_name = params[:table_name]
@rows = Table.all #display them


, , , , . , 2 2 .

?

  • ActiveRecord BBS-
+1
2

, :
Class.new(AR::Base)

post_class = Class.new(ActiveRecord::Base)
post_class.connection = set_up_connection()
post_class.table_name = :posts

@posts = post_class.all
puts @posts

# note: post_class will get GC'ed at scope end just like any var, sweet!
+3

Rails , HTTP- . apache,

, adadsik

0

All Articles