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控制台irb命令时出现此错误,我迷路了,请帮忙。我也在想我可能需要使用很多:通过。也欢迎对我的数据模型提出批评。

这是错误: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 回答