ActiveRecord has_many关联NameError:未初始化的常量

时间:2015-03-06 00:07:28

标签: ruby-on-rails activerecord

我有两个型号

class DistributionResults < ActiveRecord::Base
  belongs_to :eqadistribution
  belongs_to :meter
  belongs_to :user

class User < ActiveRecord::Base
  belongs_to :prime_contractor
  has_many :distribution_results
  has_many :addresses
  has_many :meters
  has_many :device_capabilities, :through => :meters
  has_many :email_addresses

除用户外,所有关联和相关功能似乎都按预期工作 - &gt; distribution_results has_many / belongs_to

在尝试创建user.distribution_result时,我收到错误:

NameError:        未初始化的常量User :: DistributionResult

我在distribution_results表中有一个user_id字段。

完整的错误跟踪是:

NameError: uninitialized constant User::DistributionResult
from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activerecord/lib/active_record/inheritance.rb:158:in `compute_type'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activerecord/lib/active_record/reflection.rb:271:in `compute_class'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activerecord/lib/active_record/reflection.rb:267:in `klass'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activerecord/lib/active_record/associations/association.rb:118:in `klass'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activerecord/lib/active_record/associations/collection_association.rb:39:in `reader'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activerecord/lib/active_record/associations/builder/association.rb:115:in `distribution_results'
    from (irb):7
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/railties/lib/rails/commands/console.rb:110:in `start'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/railties/lib/rails/commands/console.rb:9:in `start'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/railties/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/railties/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/railties/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:274:in `require'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:274:in `require'
    from /Users/craigwebster/bolton-eqa-website/bin/rails:8:in `<top (required)>'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:268:in `load'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/craigwebster/.rvm/gems/ruby-2.2.0/bundler/gems/rails-e43545794963/activesupport/lib/active_support/dependencies.rb:268:in `load'
    from /Users/craigwebster/.rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/craigwebster/.rvm/rubies/ruby-2.2.0/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

我正在尝试在用户模型

中实现before destroy回调方法
before_destroy :check_any_email_addresses, :check_any_meters, :check_any_distribution_results, :check_any_addresses

def check_any_email_addresses   
if email_addresses.any?     
  self.errors[:base] << "Cannot delete Prime Contractor while an associated email exists."
  return false   
end 
end 

def check_any_meters   
  if meters.any?     
    self.errors[:base] << "Cannot delete Prime Contractor while an associated meter exists."
    return false   
  end 
end 

def check_any_distribution_results   
  if distribution_results.any?     
    self.errors[:base] << "Cannot delete Prime Contractor while an associated distribution result exists."
    return false   
  end 
end 

def check_any_addresses   
  if addresses.any?     
    self.errors[:base] << "Cannot delete Prime Contractor while an associated address exists."
    return false   
  end 
end 

所有其他回调方法都可以正常工作

1 个答案:

答案 0 :(得分:1)

更新

我刚想到你的模型是复数。它应该是DistributionResult,其中包含表格distribution_results

将您的电话改为:

user.distribution_results

注意结果的复数。这是因为user has_many distribution_results