interesting trace from search - 401ChemistryGenealogy/ChemistryGenealogy GitHub Wiki

@persons = Set.new
@concrete_institutions = Set.new

@mentors = Person.select('people.name, people.id, people.institution_id')
           .joins('LEFT OUTER JOIN mentorships ON mentorships.mentor_id = people.id')
           .where('mentorships.person_id' => person_id).where('approved' => true)

@persons.add(@mentors)

@mentored = Person.select('people.name, people.id, people.institution_id')
            .joins(:mentorships)
            .where('mentorships.mentor_id' => person_id).where('approved' => true)
@persons.add(@mentored)

@supervisors = Person.select('people.name, people.id, people.institution_id')
               .joins('LEFT OUTER JOIN supervisions ON supervisions.supervisor_id = people.id')
               .where('supervisions.person_id' => person_id).where('approved' => true)
@persons.add(@supervisors)

@supervised = Person.select('people.name, people.id, people.institution_id')
              .joins(:supervisions)
              .where('supervisions.supervisor_id' => person_id).where('approved' => true)
@persons.add(@supervised)

# this code is not executed "proceduraly"!
@persons.each do |p|
  p.each do |person|
    puts 'next line ran here'
    unless person.institution_id == nil
      @concrete_institutions.add(person.institution_id)
    end
  end
end

One would expect to see the four queries ran and then see four next line ran here prints, but as one can see from the execution in the rails console the person.instituion_id is added to @concrete_institutions.add(person.institution_id) directly after each query is ran... when the data is presumably in memory. A runtime optimization?

2.3.0 :105 > Search.relations_by_id(1)
  Person Load (0.2ms)  SELECT people.name, people.id, people.institution_id FROM "people" LEFT OUTER JOIN mentorships ON mentorships.mentor_id = people.id WHERE "mentorships"."person_id" = 1 AND "people"."approved" = ?  ["approved", "t"](/401ChemistryGenealogy/ChemistryGenealogy/wiki/"approved",-"t")
next line ran here
next line ran here
  Person Load (0.2ms)  SELECT people.name, people.id, people.institution_id FROM "people" INNER JOIN "mentorships" ON "mentorships"."person_id" = "people"."id" WHERE "mentorships"."mentor_id" = 1 AND "people"."approved" = ?  ["approved", "t"](/401ChemistryGenealogy/ChemistryGenealogy/wiki/"approved",-"t")
  Person Load (0.2ms)  SELECT people.name, people.id, people.institution_id FROM "people" LEFT OUTER JOIN supervisions ON supervisions.supervisor_id = people.id WHERE "supervisions"."person_id" = 1 AND "people"."approved" = ?  ["approved", "t"](/401ChemistryGenealogy/ChemistryGenealogy/wiki/"approved",-"t")
next line ran here
  Person Load (0.2ms)  SELECT people.name, people.id, people.institution_id FROM "people" INNER JOIN "supervisions" ON "supervisions"."person_id" = "people"."id" WHERE "supervisions"."supervisor_id" = 1 AND "people"."approved" = ?  ["approved", "t"](/401ChemistryGenealogy/ChemistryGenealogy/wiki/"approved",-"t")
next line ran here
  Institution Load (0.1ms)  SELECT id, name FROM "institutions" WHERE "institutions"."id" IN (1, 3)

It's probably nothing too special...