2010-11-09 76 views
2

我正在使用Ruby on Rails 2.3.10。我有一堂課,School。我用STI給我幾個小類:PrimarySchool,SecondarySchoolUniversity。如預期在Rails中使用STI時ActiveRecord :: SubclassNotFound錯誤

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.firstPrimarySchool.last工作。 SecondarySchool.firstSecondarySchool.last按預期工作。 University.last作品。

然而,University.firstUniversity.allActiveRecord::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 

我缺少/做錯了嗎?

回答

3

這是開發模式中的known issue

一種解決方法是在基類文件的底部註冊子類。

%w(secondary_school university).each do |r| 
    require_dependency r 
end if Rails.env.development? 
+0

class School 2010-11-09 22:04:30

+0

這對我不起作用。還嘗試了./script/console製作並仍然看到錯誤。 – 2010-11-09 22:05:13

+0

這對我來說很合適,但現在我需要記得在我更改所說的子類時重新啓動rails server。謝謝。 – 2012-01-06 06:10:06

2

KandadaBoggu讓我走上正軌。皮特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 ,改變類名和更新類型列使得這個工作。我很困惑。

相關問題