简体   繁体   English

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

[英]ActiveRecord has_many association NameError: uninitialized constant

I have two models 我有两个模型

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

And

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

All associations and associated functions appear to work as expected except for the user -> distribution_results has_many / belongs_to 除了用户-> distribution_results has_many / belongs_to之外,所有关联和关联函数都按预期运行

On trying to create a user.distribution_result I get the error: 在尝试创建user.distribution_result时出现错误:

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

I have a user_id field in the distribution_results table. 我在distribution_results表中有一个user_id字段。

Full trace of error is: 完整的错误跟踪是:

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'

And I'm trying to implement a before destroy call back method in the User Model 我正在尝试在用户模型中实现之前销毁回调方法

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 

All the other callback methods work fine 所有其他回调方法都可以正常工作

Update: 更新:

It just occurred to me that your model is plural. 在我看来,您的模型是复数的。 It should be DistributionResult which has the table distribution_results . 应该DistributionResult具有表distribution_results

Change your call to: 将您的通话更改为:

user.distribution_results

Note the pluralization of result. 注意结果的多元性。 This is because a user has_many distribution_results 这是因为user has_many distribution_results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 has_many中的NameError(未初始化的常量ApplicationRecord)通过关联 - NameError (uninitialized constant ApplicationRecord) in has_many through association has_many通过关联| NameError:未初始化的常量Organization :: Organizationsuser - has_many through association | NameError: uninitialized constant Organization::Organizationsuser Rails:为什么“has_many ..., :through =&gt; ...”关联会导致“NameError: uninitialized constant ...” - Rails: Why “has_many …, :through => …” association results in “NameError: uninitialized constant …” 使用has_many时未初始化的常量NameError - uninitialized constant NameError when use has_many NameError:通过关系的has_many的未初始化常量 - NameError: uninitialized constant for has_many through relationship Rails活动记录:has_many错误(NameError:未初始化的常量) - Rails Active Record: has_many error (NameError: uninitialized constant) has_many中的Rails NameError未初始化常量:through关系 - Rails NameError uninitialized constant in has_many :through relation 通过关联对has_many进行未初始化的常量错误 - Uninitialized constant error on has_many through association NameError:未初始化的常量 - activerecord - NameError: uninitialized constant — activerecord has_many:通过关联会导致NameError - has_many :through association results in NameError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM