0

私はこれらのモデルを持っています:

class Segment < ActiveRecord::Base
  has_many :families, :foreign_key => :segment_code, :primary_key => :code
end

class Family < ActiveRecord::Base
  belongs_to :segment, :foreign_key => :segment_code, :primary_key => :code
  has_many :genus, :foreign_key => :family_code, :primary_key => :code
end

class Genus < ActiveRecord::Base
  belongs_to :family, :foreign_key => :family_code, :primary_key => :code
  has_many :commodities, :foreign_key => :genus_code, :primary_key => :code
end

class Commodity < ActiveRecord::Base
  belongs_to :genus, :foreign_key => :genus_code, :primary_key => :code
end

そして、この一連のrails console irbコマンドを呼び出すと、このエラーが発生します。迷子になっています。助けてください。私はまた、私が使用する必要があるかもしれないと思っています:throughがたくさんあります。私のデータモデルに対する批評も歓迎します。

これはエラーです:NameError: uninitialized constant Family::Genu

irb(main):003:0> @genus = Genus.first
  ←[1m←[35mGenus Load (15.6ms)←[0m  SELECT `genus`.* FROM `genus` LIMIT 1
=> #<Genus key: 100003, family_code: 10100000, code: 10101500, title: "Livestock", defintion: nil>
irb(main):004:0> @genus.family
  ←[1m←[36mFamily Load (0.0ms)←[0m  ←[1mSELECT `families`.* FROM `families` WHERE `families`.`code` = 10100000 LIMIT 1←[0m
=> #<Family key: 100002, segment_code: 10000000, code: 10100000, title: "Live animals", definition: nil>
irb(main):005:0> @family = @genus.family.first
NoMethodError: undefined method `first' for #<Family:0x2621a60>
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activemodel-3.1.0/lib/active_model/attribute_methods.rb:385:in `method_missing'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/attribute_methods.rb:60:in `method_missing'
        from (irb):5
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):006:0> @family = @genus.family
=> #<Family key: 100002, segment_code: 10000000, code: 10100000, title: "Live animals", definition: nil>
irb(main):007:0> @family.genus
NameError: uninitialized constant Family::Genu
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:1335:in `compute_type'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/reflection.rb:173:in `klass'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:118:in `klass'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:166:in `find_target?'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:323:in `load_target'
        from C:in `load_target'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):008:0>
4

1 に答える 1

2

Genus / Genera(Genu / Genusではない)の複数形と混同され、「s」を削除して単数形にします。config / initializers/inflections.rbでこれを修正してください

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'genus', 'genera'
end

その場合、has_manyを変更する必要があります。

class Family < ActiveRecord::Base
  belongs_to :segment, :foreign_key => :segment_code, :primary_key => :code
  has_many :genera, :foreign_key => :family_code, :primary_key => :code
end

または、これを行うこともできます。これは正しい英語ではありませんが、既存のコードを機能させることができます。

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( genus )
end
于 2012-05-31T20:56:19.220 に答える