Skip to content

Instantly share code, notes, and snippets.

@tdm00
Created October 1, 2012 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdm00/3814822 to your computer and use it in GitHub Desktop.
Save tdm00/3814822 to your computer and use it in GitHub Desktop.
Associations to deep?
class Patient < ActiveRecord::Base
# Associations
has_many :Requisitions
# Functions
def full_name
return [given_name, family_name].join(' ')
end
end
# This correctly finds the record
rt = RequisitionTest.find(323)
# This correctly displays the patients name
rt.Requisition.Patient.full_name
# This fails with undefined method `datereported' for #<ActiveRecord::Relation:0x00000000000000>
# However doing rt.RequisitionTestReports shows the datereported field with a date/time value
rt.RequisitionTestReports.datereported
# This also fails with undefined method `ReportType' for #<ActiveRecord::Relation:0x00000000000000>
rt.RequisitionTestReports.ReportType.name
# However this works
r = RequisitionTestReport.find(420)
r.ReportType.name
class ReportType < ActiveRecord::Base
# Associations
has_many :RequisitionTestReports
end
class Requisition < ActiveRecord::Base
# Associations
has_many :RequisitionTests
belongs_to :Patient
end
class RequisitionTest < ActiveRecord::Base
# Associations
belongs_to :Requisition
has_many :RequisitionTestReports
end
class RequisitionTestReport < ActiveRecord::Base
# Associations
belongs_to :RequisitionTest
belongs_to :ReportType
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment