I have two associated models, employee and doctor:
class Employee < ActiveRecord::Base
has_one :doctor, :dependent => :destroy
...
end
class Doctor < ActiveRecord::Base
belongs_to :employee
end
When the doctor has already been created and saved.
I want to get access to doctors id in employees after_save callback:
class Employee < ActiveRecord::Base
...
after_save :save_picture
...
private
def save_picture
if doctor
file_name = 'doctor_' + doctor.id.to_s + '.jpg'
...
end
end
end
I can get access to any doctors method and any of them works fine, but “id” – it returns “nil”.
What am I doing wrong?
I found out where my mistake was:
I had a bad accessor in
EmployeeclassIt was trying to create a new doctor, but it fault on update action. After that, doctors id attribute in ‘self’-object was empty.
The solution was to place association manipulations to another
after_savecallback: