# Setup relationship external database

**URL:** <https://www.ruby-forum.com/t/setup-relationship-external-database/131245>\
**Category:** Rails\
**Created:** [March 7, 2008, 9:25am UTC](https://www.ruby-forum.com/t/setup-relationship-external-database/131245 "2008-03-07T09:25:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [March 7, 2008, 9:25am UTC](https://www.ruby-forum.com/t/setup-relationship-external-database/131245/1 "2008-03-07T09:25:44Z")

</div>

Hi…my company has a central database. I want to use some data in my  
rails-app.

This is my setup:

class External \< ActiveRecord::Base  
establish\_connection :extern  
end

class Land \< External  
has\_and\_belongs\_to\_many :bestemming  
set\_table\_name “landen”  
set\_primary\_key “landcode”  
end

class Bestemming \< External  
has\_and\_belongs\_to\_many :land  
set\_table\_name “bestemmingen”  
set\_primary\_key “bestemming\_id”  
end

Field | Type | Null | Key | Default | Extra |  
±--------------±-------------±-----±----±--------±------+  
| landcode | char(2) | NO | PRI | | |  
| naam | varchar(50) | NO | | | |  
| naam\_en | varchar(50) | NO | | | |  
| alt\_naam | varchar(200) | NO | | | |  
| landafk | char(3) | YES | | NULL | |  
| land\_zoeknaam | varchar(100) | NO | MUL | | |  
±--------------±-------------±-----±----±--------±------+

class Bestemming \< External  
has\_and\_belongs\_to\_many :land  
set\_table\_name “bestemmingen”  
set\_primary\_key “bestemming\_id”  
end

Field | Type | Null | Key | Default | Extra |  
±--------------±-------------±-----±----±--------±------+  
| bestemming\_id | char(3) | NO | PRI | | |  
| naam | varchar(50) | YES | | NULL | |  
| naam\_en | varchar(50) | YES | | NULL | |  
| landcode | char(2) | YES | MUL | NULL | |  
| airport\_name | varchar(100) | YES | | NULL | |  
| alt\_naam | varchar(50) | YES | | NULL | |  
| dist\_ams | decimal(6,1) | YES | | NULL | |  
| stad\_zoeknaam | varchar(50) | NO | MUL | | |

When i fire-up my console

> > var = Land.find(:first)  
> > oke!  
> > var.bestemming.naam  
> > not oke!!

What i’am doing wrong here!!

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [March 8, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/setup-relationship-external-database/131245/2 "2008-03-08T20:29:34Z")

</div>

You need to add “self.abstract\_class = true” to the External class.  
Without that, Rails will assume you’re using Single Table Inheritance  
(STI).

On Mar 7, 3:24 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [March 8, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/setup-relationship-external-database/131245/3 "2008-03-08T21:25:11Z")

</div>

Remco S. wrote:

> When i fire-up my console
> 
> > > var = Land.find(:first)  
> > > oke!  
> > > var.bestemming.naam  
> > > not oke!!
> 
> What i’am doing wrong here!!

Besides “not oke” what does the error log say, my guess is that either  
you are missing your link(association) table or it’s named something  
which rails can’t read…

The default link table name for a habtm relationship is table1\_table2s  
where they are ordered alphabetically.

:has\_and\_belongs\_to\_many has options to specify the link table…

lastly, you should be aware that habtm is deprecated if the link table  
has any fields in addition to the foreign keys. If that is your case  
then please look at the rails api and the :through option on :has\_many

Oh… the link table can be in either db… but please let rails know  
which one… 🙂

hth

ilan

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [March 10, 2008, 9:20am UTC](https://www.ruby-forum.com/t/setup-relationship-external-database/131245/4 "2008-03-10T09:20:43Z")

</div>

Ilan B. wrote:

> Remco S. wrote:
> 
> > When i fire-up my console
> > 
> > > > var = Land.find(:first)  
> > > > oke!  
> > > > var.bestemming.naam  
> > > > not oke!!
> > 
> > What i’am doing wrong here!!
> 
> Besides “not oke” what does the error log say, my guess is that either  
> you are missing your link(association) table or it’s named something  
> which rails can’t read…
> 
> The default link table name for a habtm relationship is table1\_table2s  
> where they are ordered alphabetically.
> 
> :has\_and\_belongs\_to\_many has options to specify the link table…
> 
> lastly, you should be aware that habtm is deprecated if the link table  
> has any fields in addition to the foreign keys. If that is your case  
> then please look at the rails api and the :through option on :has\_many
> 
> Oh… the link table can be in either db… but please let rails know  
> which one… 🙂
> 
> hth
> 
> ilan

The table bestemming has colum (not primary key) called “landcode”. The  
table landcode has a column called landcode (primary key). This is the  
link between these tables.

I cannot change column-names because this is our central-database and i  
use this data in my rails-app.

script/console:

Find the first destination:

> > c = Bestemming.find(:first)  
> > =\> #\<Bestemming:0xb7189b08 @attributes={“airport\_name”=\>“Lehigh Valley  
> > Intl Arpt”, “landcode”=\>“US”, “alt\_naam”=\>"", “bestemming\_id”=\>“ABE”,  
> > “dist\_ams”=\>“5948.3”, “naam\_en”=\>“Allentown”, “naam”=\>“Allentown”,  
> > “stad\_zoeknaam”=\>“allentown”}\>

Find the country (link table landcode)

> > c.land.naam  
> > =\> “Verenigde Staten”

This works!!

Find the first country

> > r = Land.find(:first)  
> > =\> #\<Land:0xb717f9dc @attributes={“landcode”=\>“AF”, “alt\_naam”=\>"",  
> > “landafk”=\>“AFG”, “land\_zoeknaam”=\>“afghanistan”,  
> > “naam\_en”=\>“Afghanistan”, “naam”=\>“Afghanistan123”}\>

Find the referenced destinations of the first country

> > r.bestemming.naam

error\>\>

NoMethodError: undefined method `naam' for Bestemming:Class from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/base.rb:1233:in`method\_missing’  
from  
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/associations/has\_and\_belongs\_to\_many\_association.rb:94:in  
`send' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb:94:in`method\_missing’  
from  
./script/…/config/…/config/…/vendor/rails/activerecord/lib/active\_record/base.rb:943:in  
`with_scope' from ./script/../config/../config/../vendor/rails/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb:93:in`method\_missing’  
from (irb):6  
from :0

i would be great if you can help me

Grtz…remco
