remco.zwaan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Mar-07 08:24 UTC
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 | +---------------+--------------+------+-----+---------+-------+ | 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!! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, "remco.zw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <remco.zw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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!!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.. :) hth ilan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.. :) > > hth > > ilanThe 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.naamerror>> 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---