Rails:无法使用 STI 加载表的固定装置(未定义的方法“reflect_on_all_associations”)

发布于 2024-10-07 02:44:51 字数 2910 浏览 0 评论 0原文

我无法为使用 STI 的桌子加载装置。

当我调用 FIXTURES=schools rake db:fixtures:load 时,出现此错误:

undefined method `reflect_on_all_associations' for Object:Class
  • 其他装置加载正常,即 FIXTURES=committes rake db:fixtures:load
  • 装置是通过调用rake db:fixtures:extract创建

这是我的学校迁移:

class CreateSchools < ActiveRecord::Migration
  def self.up
    create_table :schools do |t|
      t.string :name
      t.string :type
      t.string :street
      t.string :city
      t.string :province
      t.timestamps
    end
  end
...
end

我有一些模型文件,即school.rb

class School < ActiveRecord::Base
end

secondary_school.rb

class SecondarySchool < School
end

这是堆栈跟踪:

undefined method `reflect_on_all_associations' for Object:Class
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:597:in `insert_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:568:in `each'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:568:in `insert_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:512:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:512:in `each'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:512:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:510:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/connection_adapters/mysql_adapter.rb:267:in `disable_referential_integrity'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:501:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/base.rb:1482:in `silence'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:500:in `create_fixtures'
/home/loranaw/ruby/gems/gems/rails-2.3.10/lib/tasks/databases.rake:223
/home/loranaw/ruby/gems/gems/rails-2.3.10/lib/tasks/databases.rake:222:in `each'
/home/loranaw/ruby/gems/gems/rails-2.3.10/lib/tasks/databases.rake:222

以及一些示例 YML:

--- 
schools_7133: 
  city: Nahanni Butte
  name: Charles Yohin School
  created_at: 2010-11-26 02:36:39
  grade_range: K-10
  latitude: "61.03349"
  country: CA
  updated_at: 0000-00-00 00:00:00
  postal_code: "X0E 0N0"
  url: 
  street: ""
  id: "9682"
  type: SecondarySchool
  fax: (867) 602-2202
  phone: (867) 602-2200
  funding: 
  longitude: "-123.38642"
  school_type: 
  province: NT
  email: 

I can't load fixtures for my table that uses STI.

When I call FIXTURES=schools rake db:fixtures:load, I get this error:

undefined method `reflect_on_all_associations' for Object:Class
  • Other fixtures load fine, i.e. FIXTURES=committes rake db:fixtures:load
  • The fixture was created by calling rake db:fixtures:extract

Here's my schools migration:

class CreateSchools < ActiveRecord::Migration
  def self.up
    create_table :schools do |t|
      t.string :name
      t.string :type
      t.string :street
      t.string :city
      t.string :province
      t.timestamps
    end
  end
...
end

I have a few model files, i.e. school.rb:

class School < ActiveRecord::Base
end

And secondary_school.rb:

class SecondarySchool < School
end

Here's the stack trace:

undefined method `reflect_on_all_associations' for Object:Class
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:597:in `insert_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:568:in `each'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:568:in `insert_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:512:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:512:in `each'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:512:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:510:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/connection_adapters/mysql_adapter.rb:267:in `disable_referential_integrity'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:501:in `create_fixtures'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/base.rb:1482:in `silence'
/home/loranaw/ruby/gems/gems/activerecord-2.3.10/lib/active_record/fixtures.rb:500:in `create_fixtures'
/home/loranaw/ruby/gems/gems/rails-2.3.10/lib/tasks/databases.rake:223
/home/loranaw/ruby/gems/gems/rails-2.3.10/lib/tasks/databases.rake:222:in `each'
/home/loranaw/ruby/gems/gems/rails-2.3.10/lib/tasks/databases.rake:222

And some sample YML:

--- 
schools_7133: 
  city: Nahanni Butte
  name: Charles Yohin School
  created_at: 2010-11-26 02:36:39
  grade_range: K-10
  latitude: "61.03349"
  country: CA
  updated_at: 0000-00-00 00:00:00
  postal_code: "X0E 0N0"
  url: 
  street: ""
  id: "9682"
  type: SecondarySchool
  fax: (867) 602-2202
  phone: (867) 602-2200
  funding: 
  longitude: "-123.38642"
  school_type: 
  province: NT
  email: 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所谓喜欢 2024-10-14 02:44:51

School 对象有一个名为 type 的字段,该字段是为单表继承保留的。重命名该字段应该可以解决问题。

The School object has a field called type, which is reserved for Single Table Inheritance. renaming that field should solve the problem .

南七夏 2024-10-14 02:44:51

问题最终是其中一条记录的类型值为空白/空。确保 STI 表中的每一行都有一个类型。

The problem ended up being one of records had a blank/empty value for type. Make sure every row in your STI table has a type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文