【问题标题】:Rails Uninitialized Constant in polymorphic association多态关联中的Rails未初始化常量
【发布时间】:2012-07-27 20:20:48
【问题描述】:

我正在努力将电话号码与志愿者(以及后来的其他事物)建立多态关联,目前我遇到以下错误:

uninitialized constant HumanVolunteer::PrimaryPhone
app/controllers/human_volunteers_controller.rb:44:in `new'
app/controllers/human_volunteers_controller.rb:44:in `create'

这是我的电话号码模型:

class PhoneNumber < ActiveRecord::Base
  attr_accessible :notes, :number

  belongs_to :phone, :polymorphic => true
end

这是我的 HumanVolunteers 模型:

class HumanVolunteer < ActiveRecord::Base

  attr_accessible :firstName, :lastName, :homeaddressid, :notes, :status, :workdaddressid, :home_adr, :work_adr, :primaryPhone
  has_one :primaryPhone, :as => :phone

  def home_adr=(home_adr_arg)
    # Home ADR
    home_adr = Address.new(home_adr_arg)
    if home_adr.save 
      self.homeaddressid = home_adr.id
    end
  end

  def work_adr=(work_adr_arg)
    # Work ADR
    work_adr = Address.new(work_adr_arg)
    if home_adr.save 
      self.workaddressid = work_adr.id
    end
  end
end

还有我的电话号码和human_volunteers 架构:

表:human_volunteers

id  integer 
status  character varying(255)  
homeaddressid   integer     
workdaddressid  integer     
notes   text        
created_at  timestamp without time zone 
updated_at  timestamp without time zone 
firstName   character varying(255)      
lastName    character varying(255)  

表:电话号码

id  integer 
number  character varying(255)          
notes   text        
created_at  timestamp without time zone     
updated_at  timestamp without time zone     
phone_id    integer     
phone_type  character varying(255)

当我尝试在任何输入下创建新志愿者时发生错误,这是我当前的示例请求:

{"human_volunteer"=>{"primaryPhone"=>"5555555555",
 "firstName"=>"",
 "notes"=>"",
 "work_adr"=>{"city"=>"",
 "state"=>"",
 "zipcode"=>"",
 "line1"=>"",
 "line2"=>""},
 "home_adr"=>{"city"=>"",
 "state"=>"",
 "zipcode"=>"",
 "line1"=>"",
 "line2"=>""},
 "lastName"=>""},
 "authenticity_token"=>"RCPTxZpzytYXcDEUo0czRxpI4A3Qw1ErwcIBJ92RhLA=",
 "utf8"=>"✓"}

注意:我也有一个地址类,但我已经开始工作了,所以我没有把这篇文章弄得乱七八糟。

从论坛上的浏览来看,其他人的主要问题似乎是 plauralization,但据我所知,我已经正确地将所有内容 plauralized。

我还尝试将 phone_id 或 primaryPhone_id 添加到人类志愿者表中,但没有帮助。

非常感谢, - 肯

【问题讨论】:

    标签: ruby-on-rails polymorphism uninitialized-constant


    【解决方案1】:

    您的has_one 需要知道它所指的类。

    has_one :primary_phone, :class_name => "PhoneNumber", :as => :phone

    【讨论】:

    • 谢谢,你能告诉我那是在哪里记录的吗,我查看了 rails 的东西,没有看到任何关于它的东西。我看到它的唯一地方是“2.10 Self Joins”。而且它仍然没有得到纠正,因为现在我得到了PhoneNumber(#70073652768560) expected, got String(#70073736791040)
    • 好的,我想我知道发生了什么,现在与此问题无关。我会继续调查谢谢你的帮助。
    猜你喜欢
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多