Banging my head on this problem for the past 2 hours. I have these two classes which are polymorphically linked. Doctors have doctor-y stuff in them and Users handle the web component (email addresses, devise, etc). A Doctor is a User.
The reason is there are different User classes (Nurses, MedicalInstitutions), all requiring their own fields, but are linked together by the fact that they are users. So I chose polymorphic association in this case.
The problem is I cannot seem to instantiate both of them because both of them doesn't get saved. Assume an empty database how exactly do I create both of them because they need each other? Is there a way to "save both" at the same time.
class User < ActiveRecord::Base
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :profile
belongs_to :profile, polymorphic: true
end
class Doctor < ActiveRecord::Base
has_one :user, as: :profile, dependent: :destroy
accepts_nested_attributes_for :user
validates_presence_of :user
# doctor-specific code here
delegate :email, :password, to: :user
end
class Nurse < ActiveRecord::Base
# has_one, accepts... same as doctor. nurse-specific code here
end
I'd also appreciate it if someone can solve how to instnantiate this in FactoryGirl.
Aside: I've seen this, but they don't have validations on both sides.