Rails Uninitialized在多态关联中的常量

时间:2012-07-27 20:20:49

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

我正在努力将电话号码与志愿者(以及其他事情)进行多态关联,目前我遇到了以下错误:

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

这是我的PhoneNumbers模型:

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

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)  

表:phone_numbers

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"=>"✓"}

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

从浏览论坛看,似乎其他人的主要问题是人工化,但据我所知,我已经完全正确地解决了一切问题。

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

非常感谢, - 肯

1 个答案:

答案 0 :(得分:9)

您的has_one需要知道哪个班级指的是哪个。

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