Forum: Ruby on Rails Connection problem with a generic-runtime-built ActiveRecord

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Saiho Y. (Guest)
on 2006-01-11 17:31
(Received via mailing list)
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
http://mail.yahoo.com
This topic is locked and can not be replied to.