Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:33:11+00:00 2026-05-25T11:33:11+00:00

I have two associated models, employee and doctor: class Employee < ActiveRecord::Base has_one :doctor,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:33:11+00:00Added an answer on May 25, 2026 at 11:33 am

    I found out where my mistake was:

    I had a bad accessor in Employee class

    class Employee < ActiveRecord::Base
      ...
      def is_doctor= val
        create_doctor if val == '1'
      end
      ...
    end
    

    It 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_save callback:

    class Employee < ActiveRecord::Base
      has_one :doctor, :dependent => :destroy
      after_save :save_roles
      after_save :save_picture
      ...
      def is_doctor= val
        @doc = true if val == '1'
      end
    
      private
    
      def save_roles
        if doctor
          doctor.destroy unless @doc
        else
          create_doctor if @doc
        end
        @doc = nil
      end
    
      def save_picture
        if doctor
          file_name = 'doctor_' + doctor.id.to_s + '.jpg'
          ...
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Models here associated as such: class Order < ActiveRecord::Base belongs_to :customer
My application has two associated models: Magazine and Article: class Magazine < ActiveRecord::Base has_one
I have two dependent models with one to many association class Post < ActiveRecord::Base
I have two models, Page and PageContent. class Page < ActiveRecord::Base has_many :page_contents end
I have two models that are associated with each other. Customer has_one :primary_contact And
I have two associated models (meetings and departments). A meeting has exactly one department,
I have two models, Computer and Ipv6Address Computer belongs_to Ipv6Address Ipv6Address has_one Computer Using
I have two models which are linked in a has_one / belongs_to association; Computer
So, I have two models, User and Thing. User has_many :things end Thing belongs_to
I have two models : class Game before_save :update_teacher teacher end def update_teacher teacher.update_attribute(something,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.