Counting how many questions in each category are tagged
Clash 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)
performance beginner ruby ruby-on-rails active-record
add a comment |Â
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)
performance beginner ruby ruby-on-rails active-record
add a comment |Â
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)
performance beginner ruby ruby-on-rails active-record
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)
performance beginner ruby ruby-on-rails active-record
edited Apr 29 at 16:26
200_success
123k14142399
123k14142399
asked Apr 29 at 11:15
mazing
111
111
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password