Multiple physicians -( i.e. primary , secondary , referring ) . Where to keep them in fhir - hl7

I am looking for the right place to keep multiple physicians.
So far the choices are :
DiagnosticOrder.orderer = Physicians
Patient.CareProvider = Physicians
Encounter.Participant = Physicians
Can you please suggest the right place to keep different physicians?

For DSTU2, care teams are handled using CarePlan.

Related

How do I find a specific record with a params[] in a resulting query found using multiple joins

I currently have a complex query which successfully returns multiple records for a complete list. This query contains LEFTJOINS including a LEFTJOIN to itself. I now am creating a report which needs to allow the selection of a specific record in the group. I am having difficulty discovering how to form this request when there are multiple joins including left joins. The returned parameter is in params[:search]. The query I am trying to use is:
#horses = Horse.find_by_sql["SELECT horses.id, horses.horse_name, horses.registration_number, horses.registration_number_2, horses.sire_name, horses.dam_name, horses_1.horse_name AS sire_name, horses_2.horse_name AS dam_name, horses.foaling_date, breeds.breed_name, colours.colour_name, genders.gender_name, owners.last_name, horses.arrival_date, horses.date_left, horses.notes FROM (((((horses LEFT JOIN horses AS horses_1 ON horses.sire_id = horses_1.id) LEFT JOIN horses AS horses_2 ON horses.dam_id = horses_2.id) LEFT JOIN breeds ON horses.breed_id = breeds.ID) LEFT JOIN colours ON horses.colour_id = colours.ID) LEFT JOIN genders ON horses.gender_id = genders.ID) LEFT JOIN Owners ON horses.owner_id = Owners.ID WHERE horses.id = ? ORDER BY horses.horse_name", params["search"]]
Updated after questions below:
There is no real advantage to listing the tables since the query works fine without the search parameter, in fact it is the query I use to produce the horse list. Adding the params["search"] is causing the issue, I have confirmed that it returns the correct value using debugger: params["search"] = 5, the intended id.
The error I get gives me a suggestion which is the same as I have done already. I just must be doing something really stupid that I just can't see.
Error Message:
wrong number of arguments (0 for 1..2) suggestion: # Post.find_by_sql ["SELECT title FROM posts WHERE author = ?", start_date]
Thanking you in advance
The answer to this is: There should have been a space between the "find_by_sql" and the "[". When I put this space in, it works fine.

Mongoid: Return documents related to relation?

Say I'm modeling Students, Lessons, and Teachers. Given a single student enrolled in many lessons, how would I find all of their teachers of classes that are level 102? For that matter, how would I find all of their lessons' teachers? Right now, I have this:
s = Mongoid::Student.find_by(name: 'Billy')
l = s.lessons.where(level: 102)
t = l.map { |lesson| lesson.teachers }.flatten
Is there a way to do turn the second two lines into one query?
Each collection requires at least one query, there's no way to access more than one collection in one query (i.e. no JOINs) so that's the best you can do. However, this:
t = l.map { |lesson| lesson.teachers }.flatten
is doing l.length queries to get the teachers per lesson. You can clean that up by collecting all the teacher IDs from the lessons:
teacher_ids = l.map(&:teacher_ids).flatten.uniq # Or use `inject` or ...
and then grab the teachers based on those IDs:
t = Teacher.find(teacher_ids)
# or
t = Teacher.where(:id.in => teacher_ids).to_a
If all those queries don't work for you then you're stuck with denormalizing something so that you have everything you need embedded in a single collection; this would of course mean that you'd have to maintain the copies as things change and periodically sanity check the copies for consistency problems.

Please help me with think of a better algorithm in Ruby on Rails

I have some Events, People. There is a many-to-many relationship between them so there is a PersonEvent connecting Events to People.
Event has a date and type
PersonEvent has an event_id and a person_id
Person has a name
I'm trying to build a search form that allows the user to search by the type of an Event, and then returns a list of People who attended a Event of that type in the past and the last date they attended such an Event. This should be in the controller.
The only solution I can think of involves nested loops and will probably run very slowly. I'm definitely looping through a lot of things I don't need to be.
For each person in Person.all
For each personevent in PersonEvent.all
Add the personevent to an array if the person_event.event.type is correct
Now, loop through the array and find the event with the latest date. That's the date of the last Event attendance.
Can anyone suggest a better algorithm?
In RoR, it would be:
Person.joins(:events).where(events: { type: params[:type] })
Rails joins will create an INNER JOIN, which will discard people who don't have an associated event that meets the criteria in where.
You don't explain how your keeping the date of attendance information, so I'll leave that bit up to you.
As you have the associations already set up you should be able to do something like:
f = Person.joins(:events)
f = f.where(:events => { :type => "the_type_you_are_searching_for" })
f = f.group('people.id')
f = f.select('people.*, max(events.date) as last_event_date')
people = f.all # though you probably want to paginate really
I've done it line by line to make it easier to read in here but often you'd see the where, group and select chained together one after the other on the same line.
You need the group otherwise you'll get people returned multiple times if they have been to multiple events.
The custom select is to include the last_event_date in the results.
Why not just write a custom SQL query? It would look something like this:
SELECT * FROM person_events
INNER JOIN people ON people.id = person_events.person_id
INNER JOIN events ON events.id = person_events.event_id AND events.type = 'EventType'

EF Where clause in an Include

from this example that I stole from the net:
Company company = context.Companies
.Include("Employee.Employee_Car")
.Include("Employee.Employee_Country")
.FirstOrDefault(c => c.Id == companyID);
if I want a where clause in the include of Employee_Car for example, how do I do that? Say I just want it to retrieve blue cars.
Thanks
Short answer is you can't do it just through using the include. You will need to do a bit of joining.
So taking this tip post and the SO answer you could do something like along these lines. (Note not exactly your return type but the closest)
var companyBlueCars = from company in context.Companies
where company.Id == companyID
select new
{
company,
blueCars = from employee in company.Employees
where employee.Employee_Car.Colour == "blue"
select employee.Employee_Car
};
(I did make a couple guesses about the data structure but the idea is there.)
You will not do that because include doesn't support filtering or sorting. You must execute two separate queries to load companies and cars with their filters.

How can i show only the results that have an association in rails 3?

I am trying do a where statement that only brings up results that have that particular association.
For example:
A company has many statuses through company statuses. They can have multiple statuses that can be gold, silver, and/or bronze, or none at all. I am trying to have my results only return the companies that have a status (gold, silver, and/or bronze) and not the ones have no statuses.
Your use case isn't very well articulated, but I think what you're wanting is to find Companies that have a status that matches a specified one:
Company.includes(:statuses).where('status.name = ?', params[:status_name])
That should give you the right query, assuming a Company has_many :statuses.
From the Ruby on Rails guide on Active Record Associations:
4.2.3 How To Know Whether There’s an Associated Object?
To know whether there’s and associated object just check
association.nil?:
if #supplier.account.nil?
#msg = "No account found for this supplier"
end
http://guides.rubyonrails.org/association_basics.html#detailed-association-reference
Company.joins(:statuses).select("DISTINCT(companies.id), companies.*, statuses.*")
If you have an associative table called companies_statuses:
to retrieve all companies with at least one status
Company.where("EXISTS (select 1 from companies_statuses where companies_statuses.company_id = companies.id)")
to retrieve all companies with no status
Company.where("NOT EXISTS (select 1 from companies_statuses where companies_statuses.company_id = companies.id)")

Resources