Skip to content

Instantly share code, notes, and snippets.

Created May 14, 2015 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/51888d5fcc8406a6deb8 to your computer and use it in GitHub Desktop.
Save anonymous/51888d5fcc8406a6deb8 to your computer and use it in GitHub Desktop.
class Instance < ActiveRecord::Base
has_many :reports
has_many :exams, primary_key: 'study_instance_uid', foreign_key: 'study_inst_uid', inverse_of: :instance
has_many :patients, through: :exams
before_save :set_reading_physician, if: :should_set_reading_physician?
def reading_physician
(set_reading_physician and save!) if should_set_reading_physician?
read_attribute(:reading_physician) || ''
end
private
def should_set_reading_physician?
(
v = read_attribute :reading_physician
v.nil?
) && patient
end
def set_reading_physician
self.reading_physician = patient.reading_physician
end
end
@smathy
Copy link

smathy commented May 14, 2015

def reading_physician
  self[:reading_physician] || patient.try(:reading_physician) || ''
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment