Wednesday, November 7, 2012

Stats retrieval

Assignments can be done more than once. Every time a user submits an assignment new stats are generated. This becomes an issue when pulling stats for an assignment because there will be duplicate stats.

To fix this issue I wrote a method that pulls the latest set of stats for an assignment.

  

def self.latest_for_assignment assignment
    categories = []
    concept_groups = []
    concepts = []
    Category.all.each do |c|
      categories << Stat.where(:statable_id => c.id, :statable_type => c.class.name ,:assignment_id => assignment.id  ).last
    end
    ConceptGroup.all.each do |c|
      concept_groups << Stat.where(:statable_id => c.id, :statable_type => c.class.name, :assignment_id => assignment.id).last
    end
    Concept.all.each do |c|
      concepts << Stat.where(:statable_id => c.id, :statable_type => c.class.name, :assignment_id => assignment.id ).last
    end
    [categories,concept_groups,concepts]
  end


I also change the way the show method (in the controller) works.




def show
  @categories,@concept_groups,@concepts = Stat.latest_for_assignment @assignment
  @categories.compact!
  @concept_groups.compact!
  @concepts.compact!
end

The compact! clears nils from the arrays 

No comments:

Post a Comment