I am trying to count the number of questions in each subject and then
display the question count for each subject next to the subject name:

Model
has_many :questions

Controller
def index
@subjects = Subject.order(:title)
@question_number = Question.find(???).count
end
end

View
<a href="/.../<%= subject.id %>"><%= subject.title %> (<%=
@question_number %>)</a>

The questions table has a subject_id attribute and this is what I want
to query on

Can't understand how to find the count for each subject in the @subjects
hash and then display with the associated subject

Dave

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Hassan Schroeder at Sep 22, 2012 at 3:50 pm

    On Sat, Sep 22, 2012 at 8:04 AM, Dave Castellano wrote:
    I am trying to count the number of questions in each subject and then
    display the question count for each subject next to the subject name:
    <a href="/.../<%= subject.id %>"><%= subject.title %> (<%=
    @question_number %>)</a>
    Can't understand how to find the count for each subject in the @subjects
    hash and then display with the associated subject
    <%= subject.questions.count %> doesn't work?

    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Castellano at Sep 22, 2012 at 4:07 pm
    Thanks, was overthinking it!

    Dave

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedSep 22, '12 at 3:04p
activeSep 22, '12 at 4:07p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase