Setup relationship external database

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 |

You need to add "self.abstract_class = true" to the External class.
Without that, Rails will assume you're using Single Table Inheritance
(STI).

Remco Swoany 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.. :slight_smile:

hth

ilan

Ilan Berci wrote:

Remco Swoany 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.. :slight_smile:

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

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

i would be great if you can help me

Grtz..remco