2

Ruby on Rails 2.3.10 を使用しています。私はクラスを持っていSchoolます。私は STI を使用して、いくつかのサブクラスを提供してPrimarySchoolSecondarySchoolますUniversity

app/models/primary_school.rb:
  class PrimarySchool < School
     has_many :education_records, :foreign_key => "school_id"
  end

app/models/secondary_school.rb:
  class SecondarySchool < School
     has_many :education_records, :foreign_key => "school_id"
  end

app/models/university.rb
  class University < School
     has_and_belongs_to_many :applicants, :join_table => :applicants_schools, :foreign_key => "school_id"
  end

db/migrate/20100824170203_create_schools.rb:
class CreateSchools < ActiveRecord::Migration
  def self.up
    create_table :schools do |t|
      t.string :name
      t.string :type     # secondary, cegep, college, university
      t.string :street
      ...
      t.timestamps
    end
  end

  def self.down
    drop_table :schools
  end
end

PrimarySchool.first期待どおりにPrimarySchool.last動作します。SecondarySchool.first期待どおりにSecondarySchool.last動作します。University.last動作します。

ただし、例外を発生University.firstUniversity.allせます。ActiveRecord::SubclassNotFound

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'University'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite University.inheritance_column to use another column for that information.
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:1671:in `instantiate'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:665:in `find_by_sql'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:665:in `collect!'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:665:in `find_by_sql'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:1582:in `find_every'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:619:in `find'
    from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:639:in `all'
    from (irb):6
    from :0

生成された SQL は正しいです。

  SecondarySchool Load (0.3ms)   SELECT * FROM `schools` WHERE ( (`schools`.`type` = 'University' ) ) LIMIT 1
  SecondarySchool Load (1.7ms)   SELECT * FROM `schools` WHERE ( (`schools`.`type` = 'University' ) ) ORDER BY schools.id DESC LIMIT 1

私は何が欠けていますか/間違っていますか?

4

2 に答える 2

3

これは、開発モードでの既知の問題です。

1 つの回避策は、基本クラス ファイルの下部にサブクラスを登録することです。

%w(secondary_school university).each do |r| 
  require_dependency r
end if Rails.env.development?
于 2010-11-09T21:53:13.970 に答える
2

KandadaBoggu は私を正しい軌道に乗せました。Pete P.の回避策は私にとってはうまくいきます:

class School < ActiveRecord::Base
  def self.subclasses
    [PrimarySchool, SecondarySchool, OtherSchool, University]
  end
end

例外を取り除くという点で...しかし、間違ったSQLを生成します:

SELECT * FROM `schools` WHERE ( (`schools`.`type` = 'University' OR
`schools`.`type` = 'PrimarySchool' OR `schools`.`type` = 'SecondarySchool' OR 
`schools`.`type` = 'OtherSchool' OR `schools`.`type` = 'PostSecondaryInstitution' OR 
`schools`.`type` = 'Cegep' OR `schools`.`type` = 'College' OR `schools`.`type` = 
'University' ) ) LIMIT 1

University.rb の名前を uni.rb に変更し、クラス名を変更し、タイプ列を更新すると、この作業が行われました。私は本当に混乱しています。

于 2010-11-09T22:17:06.517 に答える