1

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

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。

I also tried adding a phone_id or primaryPhone_id to the human volunteers table but it didnt help.

Thank you very much, - Ken

4

1 回答 1

13

Your has_one needs to know which class its refering to.

  has_one :primary_phone, :class_name => "PhoneNumber", :as => :phone
于 2012-07-27T20:26:14.650 回答