Rails Uninitialized Constant in polymorphic association

3.9k views Asked by At

I am working on making a polymorphic association of a phone number to a volunteer (and later to other things), currently i am stuck with the following error:

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

Here is my PhoneNumbers Model:

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

  belongs_to :phone, :polymorphic => true
end

And here is my HumanVolunteers model:

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

And my schema for phone numbers and human_volunteers:

Table: 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)  

Table: 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)

The error is happening when i try to create a new volunteer under any inputs here is my current example request:

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

NOTE: i also have an address class, but i've already got that working so i didnt clutter up this post with it.

From browsing around on the forum it seemed like the main problem for others was plauralization, but as far as i can tell i've got everything plauralized correctly.

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

Thank you very much, - Ken

1

There are 1 answers

2
Rishav Rastogi On BEST ANSWER

Your has_one needs to know which class its refering to.

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