访问活动记录关系时出现奇怪错误

时间:2012-05-31 20:50:18

标签: ruby ruby-on-rails-3 activerecord

我有这些模特:

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命令序列时遇到此错误,我失去了请帮助。我也在想我可能需要使用很多:通过。对我的数据模型的批评也很受欢迎。

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

1 个答案:

答案 0 :(得分: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