app/models/section.rb
# Custom implementation as using the plain relationships in Rails will cause
# N+1 queries to figure out most recent version for each question.
Custom implementation as using the plain relationships in Rails will cause
N+1 queries to figure out most recent version for each question.
Custom implementation as using the plain relationships in Rails will cause N + 1 queries to figure out most recent version for each question .
def questions_with_most_recent
    Question.find_by_sql(["select q.*, qmv.version as max_version, qmrv.version as most_recent_version
     from questions q, section_nested_items sni,
       (select version_independent_id, MAX(version) as version
         from questions group by version_independent_id) qmv
       left join (select version_independent_id, MAX(version) as version
         from questions q where q.status = 'published'
         group by version_independent_id) qmrv USING (version_independent_id)
     where (qmv.version_independent_id = q.version_independent_id
     and sni.question_id = q.id
     and sni.section_id = :section_id)", { section_id: id }])
  end
def questions_with_most_recent Question . find_by_sql ( [ "select q.*, qmv.version as max_version, qmrv.version as most_recent_version
     from questions q, section_nested_items sni,
       (select version_independent_id, MAX(version) as version
         from questions group by version_independent_id) qmv
       left join (select version_independent_id, MAX(version) as version
         from questions q where q.status = 'published'
         group by version_independent_id) qmrv USING (version_independent_id)
     where (qmv.version_independent_id = q.version_independent_id
     and sni.question_id = q.id
     and sni.section_id = :section_id)" , { section_id : id } ] ) end
Custom implementation as using the plain relationships in Rails will cause
N+1 queries to figure out most recent version for each question.
Custom implementation as using the plain relationships in Rails will cause N + 1 queries to figure out most recent version for each question .