#  ktypnr     :integer(5)      not null, primary key
#  kmodnr     :string(5)       default(""), not null
#
class Car < ActiveRecord::Base
  set_table_name 'tecdoc120s001'
  set_primary_key "ktypnr"  
 
  belongs_to :production_line, :class_name => "ProductionLine", :foreign_key => "kmodnr"
end



#  kmodnr     :string(5)       default(""), not null, primary key
#  hernr      :integer(5)      default(0), not null
class ProductionLine < ActiveRecord::Base
  set_table_name 'tecdoc110s001'
  set_primary_key "kmodnr"  
  
  belongs_to :manufacturer, :class_name => "Manufacturer", :foreign_key => "hernr"
  has_many :cars, :class_name => "Car", :foreign_key => "kmodnr"
end
 


#  hernr      :integer(5)      default(0), not null, primary key
class Manufacturer < ActiveRecord::Base
  set_table_name 'tecdoc100s001'
  set_primary_key "hernr"  
  
  has_many :production_lines, :class_name => "ProductionLine", :foreign_key => "hernr"

  # this works, but has no finder and isn't cool, either
  has_many :sql_cars, :finder_sql => %q~
    SELECT tecdoc120s001.* FROM tecdoc120s001 
    INNER JOIN tecdoc110s001 ON tecdoc120s001.kmodnr = tecdoc110s001.kmodnr
    WHERE (tecdoc110s001.hernr = #{self.hernr})
  ~

  has_many :cars, :through => :production_lines

  # this gives:
  # ActiveRecord::StatementInvalid in 'The Manufacturer BMW should have produced at least 3 cars'
  # Mysql::Error: Unknown column 'tecdoc110s001.ktypnr' in 'on clause': 
  # SELECT count(*) AS count_all FROM `tecdoc120s001`  INNER JOIN tecdoc110s001 ON tecdoc120s001.kmodnr = tecdoc110s001.ktypnr    WHERE ((tecdoc110s001.hernr = 2324))
end
