diff --git a/app/models/template.rb b/app/models/template.rb index 7cbe010..76be4b1 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -70,12 +70,15 @@ } # Retrieves the maximum version for the array of customization_ofs passed. If customization_ofs is missing, every maximum # version for each different customization_of will be retrieved - scope :customization_ofs_with_max_version, -> (customization_ofs=nil) { - if customization_ofs.is_a?(Array) - select("MAX(version) AS version", :customization_of).where(customization_of: customization_ofs).group(:customization_of) - else - select("MAX(version) AS version", :customization_of).group(:customization_of) + scope :customization_ofs_with_max_version, -> (customization_ofs=nil, org_id=nil) { + chained_scope = select("MAX(version) AS version", :customization_of) + if customization_ofs.respond_to?(:each) + chained_scope = chained_scope.where(customization_of: customization_ofs) end + if org_id.present? + chained_scope = chained_scope.where(org_id: org_id) + end + chained_scope.group(:customization_of) } # Retrieves the latest template version, i.e. the one with maximum version for each dmptemplate_id scope :latest_version, -> (dmptemplate_ids=nil) { @@ -85,7 +88,7 @@ } # Retrieves the latest customized version, i.e. the one with maximum version for each customization_of=dmptemplate_id scope :latest_customization, -> (org_id, dmptemplate_ids=nil) { - from(customization_ofs_with_max_version(dmptemplate_ids), :current) + from(customization_ofs_with_max_version(dmptemplate_ids, org_id), :current) .joins("INNER JOIN templates ON current.version = templates.version"\ " AND current.customization_of = templates.customization_of") .where('templates.org_id = ?', org_id)