Question on polymorphic association

I have 3 models . Doctor, Patient and User
with following associations

class Doctor < ActiveRecord::Base
has_many :patients, :dependent => :destroy
has_one :user, :as => :userable
accepts_nested_attributes_for :user
end

class Patient < ActiveRecord::Base
belongs_to :doctor
has_one :user, :as => :userable
accepts_nested_attributes_for :user
end

class User < ActiveRecord::Base
belongs_to :userable, :polymorphic => true
end

I have a nested form for doctor and user as
OUTER FORM for :doctor
INNER FORM for :user
end
end

and in my users controller i have a create action as

def create
doc_attr = params[:doctor]
user_attr = doc_attr.delete(:user)
doctor = Doctor.new(doc_attr)
doctor.save!
doctor.user.create(user_attr) ### getting error on this

end

When i submit the form i get the error saying undefined method create
for nil:NilClass
I know this is relatively simple. Any help will be greatly
appreciated. Thanks in advance !!!

Ok got this working. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs