Counting how many questions in each category are tagged

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have a homepage that shows how many questions are in a certain category which is done through tagging. The functionality is working great, it tells me how many of each there are.



However, newrelic's agent is telling me that there can be anywhere from 16-24 database calls per request, which seems excessive to me. How can I make this better? I just picked up rails a month or two ago, if that is relevant.



 def index

@multiple_choice_questions = MultipleChoiceQuestion.all
@published_multiple_choice_questions = MultipleChoiceQuestion.all.published

## Get number of questions that are tagged with foundation
@foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).all
@published_foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).published

@anatomy_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["anatomy", "foundation", "sem1"], :match_all => true).published.size
@behavioral_sciences_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["behavioral science", "foundation", "sem1"], :match_all => true).published.size
@biochemistry_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["biochemistry", "foundation", "sem1"], :match_all => true).published.size
@community_medicine_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["community medicine", "foundation", "sem1"], :match_all => true).published.size
@embryology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["embryology", "foundation", "sem1"], :match_all => true).published.size
@histology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["histology", "foundation", "sem1"], :match_all => true).published.size
@microbiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["microbiology", "foundation", "sem1"], :match_all => true).published.size
@pathology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["pathology", "foundation", "sem1"], :match_all => true).published.size
@sem1_pharma_mcq = MultipleChoiceQuestion.tagged_with(["pharmacology", "sem1"], :match_all => true).published.size
@physiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["physiology", "foundation", "sem1"], :match_all => true).published.size
##
#


end


published is a scope defined in the model file, by the way.



scope :published, -> where(published: true)







share|improve this question



























    up vote
    2
    down vote

    favorite












    I have a homepage that shows how many questions are in a certain category which is done through tagging. The functionality is working great, it tells me how many of each there are.



    However, newrelic's agent is telling me that there can be anywhere from 16-24 database calls per request, which seems excessive to me. How can I make this better? I just picked up rails a month or two ago, if that is relevant.



     def index

    @multiple_choice_questions = MultipleChoiceQuestion.all
    @published_multiple_choice_questions = MultipleChoiceQuestion.all.published

    ## Get number of questions that are tagged with foundation
    @foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).all
    @published_foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).published

    @anatomy_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["anatomy", "foundation", "sem1"], :match_all => true).published.size
    @behavioral_sciences_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["behavioral science", "foundation", "sem1"], :match_all => true).published.size
    @biochemistry_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["biochemistry", "foundation", "sem1"], :match_all => true).published.size
    @community_medicine_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["community medicine", "foundation", "sem1"], :match_all => true).published.size
    @embryology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["embryology", "foundation", "sem1"], :match_all => true).published.size
    @histology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["histology", "foundation", "sem1"], :match_all => true).published.size
    @microbiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["microbiology", "foundation", "sem1"], :match_all => true).published.size
    @pathology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["pathology", "foundation", "sem1"], :match_all => true).published.size
    @sem1_pharma_mcq = MultipleChoiceQuestion.tagged_with(["pharmacology", "sem1"], :match_all => true).published.size
    @physiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["physiology", "foundation", "sem1"], :match_all => true).published.size
    ##
    #


    end


    published is a scope defined in the model file, by the way.



    scope :published, -> where(published: true)







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a homepage that shows how many questions are in a certain category which is done through tagging. The functionality is working great, it tells me how many of each there are.



      However, newrelic's agent is telling me that there can be anywhere from 16-24 database calls per request, which seems excessive to me. How can I make this better? I just picked up rails a month or two ago, if that is relevant.



       def index

      @multiple_choice_questions = MultipleChoiceQuestion.all
      @published_multiple_choice_questions = MultipleChoiceQuestion.all.published

      ## Get number of questions that are tagged with foundation
      @foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).all
      @published_foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).published

      @anatomy_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["anatomy", "foundation", "sem1"], :match_all => true).published.size
      @behavioral_sciences_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["behavioral science", "foundation", "sem1"], :match_all => true).published.size
      @biochemistry_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["biochemistry", "foundation", "sem1"], :match_all => true).published.size
      @community_medicine_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["community medicine", "foundation", "sem1"], :match_all => true).published.size
      @embryology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["embryology", "foundation", "sem1"], :match_all => true).published.size
      @histology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["histology", "foundation", "sem1"], :match_all => true).published.size
      @microbiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["microbiology", "foundation", "sem1"], :match_all => true).published.size
      @pathology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["pathology", "foundation", "sem1"], :match_all => true).published.size
      @sem1_pharma_mcq = MultipleChoiceQuestion.tagged_with(["pharmacology", "sem1"], :match_all => true).published.size
      @physiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["physiology", "foundation", "sem1"], :match_all => true).published.size
      ##
      #


      end


      published is a scope defined in the model file, by the way.



      scope :published, -> where(published: true)







      share|improve this question













      I have a homepage that shows how many questions are in a certain category which is done through tagging. The functionality is working great, it tells me how many of each there are.



      However, newrelic's agent is telling me that there can be anywhere from 16-24 database calls per request, which seems excessive to me. How can I make this better? I just picked up rails a month or two ago, if that is relevant.



       def index

      @multiple_choice_questions = MultipleChoiceQuestion.all
      @published_multiple_choice_questions = MultipleChoiceQuestion.all.published

      ## Get number of questions that are tagged with foundation
      @foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).all
      @published_foundation_mcqs = MultipleChoiceQuestion.tagged_with(["foundation"], :wild => true).published

      @anatomy_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["anatomy", "foundation", "sem1"], :match_all => true).published.size
      @behavioral_sciences_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["behavioral science", "foundation", "sem1"], :match_all => true).published.size
      @biochemistry_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["biochemistry", "foundation", "sem1"], :match_all => true).published.size
      @community_medicine_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["community medicine", "foundation", "sem1"], :match_all => true).published.size
      @embryology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["embryology", "foundation", "sem1"], :match_all => true).published.size
      @histology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["histology", "foundation", "sem1"], :match_all => true).published.size
      @microbiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["microbiology", "foundation", "sem1"], :match_all => true).published.size
      @pathology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["pathology", "foundation", "sem1"], :match_all => true).published.size
      @sem1_pharma_mcq = MultipleChoiceQuestion.tagged_with(["pharmacology", "sem1"], :match_all => true).published.size
      @physiology_sem_one_foundation_mcq = MultipleChoiceQuestion.tagged_with(["physiology", "foundation", "sem1"], :match_all => true).published.size
      ##
      #


      end


      published is a scope defined in the model file, by the way.



      scope :published, -> where(published: true)









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 29 at 16:26









      200_success

      123k14142399




      123k14142399









      asked Apr 29 at 11:15









      mazing

      111




      111

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193200%2fcounting-how-many-questions-in-each-category-are-tagged%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193200%2fcounting-how-many-questions-in-each-category-are-tagged%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?