1

database.yml looks like

development:
  adapter: mysql2
  host: localhost
  database: database1
  username: root
  password: 

external:
  adapter: mysql2
  host: localhost
  database: database2
  username: root
  password: 

I have a class ExternalDatabaseConnection

class ExternalDatabaseConnection < ActiveRecord::Base
  self.abstract_class = true
  establish_connection(:external) #connect to external DB specified in database.yml
  attr_protected
end

I'm using AR to query on my external DB.

database2(external DB) is set dynamically from user end and I will not have any information about the tables and attributes.

database1(internal DB) which contains information about tables and some attributes.


So basically I have to handle things dynamically.

Works fine.

   ExternalDatabaseConnection.table_name = "set_table1" #dynamic
   ExternalDatabaseConnection.create(...) #creates a record.

This doesn't.

ExternalDatabaseConnection.table_name = "set_different_table" #dynamic
ExternalDatabaseConnection.create(...)

throws

unknown attribute: attribute_name

Both the things happen in loop, so every-time it works good for initial table set, for the second table, it throws an error.

Debugging:

enter image description here

Note:

ExternalDatabaseConnection.table_name = "payments"

ExternalDatabaseConnection.column_names
 => ["id", "customer_id", "patent_id", "amount", "currency", "date"]

ExternalDatabaseConnection.table_name = "patents"
 => "patents" 

2.0.0-p451 :006 > ExternalDatabaseConnection.column_names
 => ["id", "customer_id", "patent_id", "amount", "currency", "date"]

ExternalDatabaseConnection.all
ExternalDatabaseConnection Load (0.4ms)  SELECT `patents`.* FROM `patents` 
=> [#<ExternalDatabaseConnection id: 1, number: "1111", description: "https://www.google.co.in", registry_date: "2014-08-20", documnetdir: nil, currency: #<BigDecimal:a8f2adc,'0.1212E2',18(27)>>, #<ExternalDatabaseConnection id: 4, number: "1", description: "jhufgtrdfyuh", registry_date: nil, documnetdir: "", currency: #<BigDecimal:a8f2258,'0.0',9(18)>>, ....]

There is no change in attributes, may be this is causing a problem.

If any information is missing, comment up.

ruby 2.0.0p451

Rails 3.2.17

edit1:

Tries:

Before setting table name

1) ExternalDatabaseConnection.table_name = nil . 2) ExternalDatabaseConnection.clear_active_connections! and clear_all_connections! 3) tried with set_table_name :table1

edit2:

ClassName.column_names is not getting changed even after setting different table_name for the Class

8
  • 1
    From the Console image which you posted,i can see you are giving ExternalDatabaseConnection.create(number: "1234") but there is no number column in the specified table which obviously gives you unknown attribute: number. Commented Aug 26, 2014 at 8:13
  • I think you meant to give ExternalDatabaseConnection.create(amount: "1234") Commented Aug 26, 2014 at 8:16
  • @Pavan check the note last point ExternalDatabaseConnection.all result. Commented Aug 26, 2014 at 8:20
  • What about the Image you posted.I don't see any column called number ExternalDatabaseConnection.column_names output. Commented Aug 26, 2014 at 8:22
  • Which specific rails version? 'Rails 3' covers over 30 versions Commented Aug 26, 2014 at 8:24

1 Answer 1

4

I have tried this following way. It may help

:set_table_name method is used to change the table name dynamically. You can set the table name and get the attributes name instead of columns name.

For payments table

ExternalDatabaseConnection.set_table_name :payments

ExternalDatabaseConnection.first.attribute_names

Then you have to reset the column information

ExternalDatabaseConnection.reset_column_information

For patents:

ExternalDatabaseConnection.set_table_name :patents

ExternalDatabaseConnection.first.attribute_names

Sign up to request clarification or add additional context in comments.

4 Comments

You have to reset the column information as well i updated the answer
Nice! Do you still need help on this nithin? I can jump into chat if so
@Hudson instead of set_table_name, table_name = is fine. As set_table_name runs query
Nice. I'll be using this when I get round to it later on!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.