08 Jan 2010, 10:13
Generic-user-small

Riccardo Tacconi (3 posts)

Hi,

I have a one to many relationship between one DB and another. This is my connectino class to the ‘legacy db’:

class Intranet2Database < ActiveRecord::Base self.abstract_class = true establish_connection(:intranet2_db) unless connected? ActiveRecord::Base.pluralize_table_names = false end

I have a model class mapping a table in the legacy database:

require ‘intranet2_db’

class Dept < Intranet2Database set_table_name :dept set_primary_key ‘deptID’

has_many :teams end

As you can see the table is dept, it is not plural, and cannot be renamed. I have a table in the new DB named teams:

class Team < ActiveRecord::Base belongs_to :dept end

and has a dept_id key that shoule map the dept.deptID primary key. When I try to acccess the dept row related to a teams row I get this:

$ script/console Loading development environment (Rails 2.3.4) » t=Team.find(1) => #<Team id: 1, name: “little_department”, dept_id: 1, created_at: “2010-01-07 18:27:0 3”, updated_at: “2010-01-07 18:27:03”> » t.dept ActiveRecord::StatementInvalid: Could not find table ‘dept’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /connection_adapters/sqlite3_adapter.rb:29:in table_structure' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_suppo rt/core_ext/object/misc.rb:39:in returning’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /connection_adapters/sqlite3_adapter.rb:28:in table_structure' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /connection_adapters/sqlite_adapter.rb:228:in columns’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /base.rb:1271:in columns' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /base.rb:1279:in columns_hash’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /base.rb:1578:in find_one' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /base.rb:1569:in find_from_ids’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /base.rb:616:in find' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /associations/belongs_to_association.rb:49:in send’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /associations/belongs_to_association.rb:49:in find_target' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /associations/association_proxy.rb:240:in load_target’ from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /associations/association_proxy.rb:112:in reload' from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record /associations.rb:1247:in dept’ from (irb):4 » t.dept_id => 1 »

So the problem is:

ActiveRecord::StatementInvalid: Could not find table ‘dept’

Can you see any error in my code?

12 Jan 2010, 12:27
Maik_schmidt_avatar2_pragsmall

Maik Schmidt (112 posts)

Hi Riccardo!

Could you, please, provide some more details:

  • What database products do you use? Obviously, it’s sqlite, but do you use another one, too, in your example?
  • How does your database.yml look like?
  • How do your migrations look like?

Your posting is in the wrong forum (it’s the forum for my first book). I’ll ask my publisher, if it’s possible to move it to the right one, so don’t be surprised, if it “disappears”.

Cheers,

Maik

  You must be logged in to comment