Hi,
I’m trying to create a “runtime-generic” ActiveRecord
class, It means I don’t need to know the table or
database, before I create my program ruby-rails. So I
don’t need to predefine my database in environment.yml
file and pre-build classes inherited from
ActiveRecord::Base.
I write some code, it seems to work. However, I can’t
disconnect my connection via “remove_connection” of a
postgresql database that I connected with
“establish_connection”
This is the generic class I wrote:
class GenActRec < ActiveRecord::Base
def self.connectToPSQLDB( hostName, portNum, dbName,
user, pwd )
ActiveRecord::Base.establish_connection(:adapter
=> “postgresql”, :host => hostName,
:port => portNum,
:database => dbName,
:username => user,
:password => pwd)
end
def self.closeConnection()
return ActiveRecord::Base.remove_connection()
end
def self.buildNewTableClass(className, tableName)
genClass = Class.new(GenActRec)
const_set(“#{className.to_s}”, genClass)
genClass.set_table_name tableName
genClass.reset_column_information()
return genClass
end
read table Column names
def readTableColumnNames
return self.class.column_names()
end
end
this how I use it:
class GentestController < ApplicationController
def index
GenActRec.connectToPSQLDB(“1.2.3.4”, 5432, “gendb”,
“dbuser” ,“dbpwd” )
test create
genAClass =
GenActRec.buildNewTableClass(“GenTableAs”,“gen_table_as”)
@name = genAClass.name
obj1 = genAClass.new
@col1 = obj1.class.column_names()
@removedConnection = GenActRec.closeConnection()
should be an error
@col2 = obj1.class.column_names()
end
end
Basically, @col2 should be an error, because the
connection should be closed, but I can still obtain
results. it is the same result as @col1, it means the
connection doesn’t close…
Someone know why?
Thanks you very much ,
saiho
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around