0

Trying to have web app change schemas based on a drop down. I have a feeling that I am not pulling in the drop down selection.

Model:

class MyApp < ActiveRecord::Base 
def initialize(dbase)
 @dbase = fdbase
 if @dbase == 'DB01'
   ActiveRecord::Base.establish_connection"#{Rails.env}"
 else
   ActiveRecord::Base.establish_connection"#{Rails.env}_#{@fc}"
end
end
end

controller:

class MyAppController < ApplicationController
before_filter :initialize_remote_user

   def self.search(name)
     @results = MyApp.search(params[:name])
     @dbase = params[:dbase].inspect
     c = MyApp.new(@dbase)
     result = c.last 
    end

   def result
     c = MyApp.new(@dbase)
     result = c.last
     @results =  MyApp.where("name = ?",params[:name]).order("item_scan_date ASC")
     puts @results.inspect
   end


   def update
    @results = MyApp.Where(name: name_params)
    @dbase = params[:dbase].inspect
    end

   def change
     @results = MyApp.Where(name: name_params)
     @dbase = params[:dbase].inspect
     end

   def show
    @dbase = params[:dbase].inspect
    c = MyApp.new(@dbase)
    result = c.last
   end

   def create
   end

   def new
   end

   def index
     @dbase = params[:dbase].inspect
     c = MyApp.new(@dbase)
     result = c.last
   end

  private

  def name_params
     params.require(:name).permit(:name)
  end

  def dbase_params
     params.require(:dbase).permit(:dbase, :id)
  end

end

the drop down is on my nav bar and pulled in in _nav.html.erb: below is the _dbform.html.erb

<div id="float_right">
<%= form_for @dbase do |f| %>
<%= f.select( :dbase, options_for_select([['DB01','DB01'],['DB02','DB02'],['DB03','DB03'],['DB04','DB04']])) %>
<% end %>

</div>

database.yml which production and development are the same.

# Uses RubyGem-activerecord-jdbcsqlite3-adapter

development:
  adapter: mysql2
  host: myhost
  database: DB01
  encoding: utf8
  port: 3306

development_DB2:
  adapter: mysql2
  host: myhost
  database: DB02
  encoding: utf8
  port: 3306

development_DB3:
  adapter: mysql2
  host: myhost
  database: DB03
  encoding: utf8
  port: 3306

development_DB4:
  adapter: mysql2
  host: myhost
  database: DB04
  encoding: utf8
  port: 3306

I am not sure what I am doing wrong.

nmvida
  • 5
  • 5

1 Answers1

0

instead of the controller code it would probably be more useful to see the actual back-trace of the error ... since from that one can likely figure out the AR and AR-JDBC versions, anyway as a blind shot try this :

if @dbase == 'DB01'
  ActiveRecord::Base.establish_connection "#{Rails.env}".to_sym
else
  ActiveRecord::Base.establish_connection :"#{Rails.env}_#{@dbase}"
end

... that is using symbols + @dbase seems to be the variable we need

kares
  • 7,076
  • 1
  • 28
  • 38