remco
1
hi,
i have 2 tables on a external database which i want to integrate in my
rails-app.
class External < ActiveRecord::Base
establish_connection :extern
end
class Land < External
set_table_name “landen”
end
class Bestemming < External
set_table_name “bestemmingen”
end
This works fine:
test = Bestemming.find(:first)
#<Bestemming:0xb701nb3c0 @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”}>
test = Land.find(:first)
<Land:0xb7018ad0 @attributes={“landcode”=>“AF”, “alt_naam”=>"",
“landafk”=>“AFG”, “land_zoeknaam”=>“afghanistan”,
“naam_en”=>“Afghanistan”, “naam”=>“Afghanistan”}>
Question: How can i realize a relationship between this classes.
Landcode is the foreign_key.
Grtz, remco
remco
2
Doesn’t this work
class Land < External
has_and_belongs_to_many :bestemmigens
set_table_name “landen”
end
class Bestemming < External
has_and_belongs_to_many :landens
set_table_name “bestemmingen”
end
remco
3
Adhiraj Rankhambe wrote:
Doesn’t this work
class Land < External
has_and_belongs_to_many :bestemmigens
set_table_name “landen”
end
class Bestemming < External
has_and_belongs_to_many :landens
set_table_name “bestemmingen”
end
Nope…
I have different primary_keys than default…because it is an central
database.
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!!
remco
4
ok so what about the join table bestemming_land_join_tables?
Does it have the keys landcode and bestemming_id?