0

We have Questions and Answers, and every Question has_many :answers and those answers belongs_to :questions.

I want to display the CURRENT count of answers on the CURRENT Question.

i tried in my Answers_Controller :

def show
  @question = Question.find(params[:question_id])
  @answers_count = @question.answers.count
end

and then called on my view <%= @answers_count %>.

But i think i'm missing something here, because nothing is Displayed.

Mini John
  • 7,855
  • 9
  • 59
  • 108

2 Answers2

1

I found out that i don't need to call those in my Controller, i can instead just call

<%= @question.answers.count %>
Mini John
  • 7,855
  • 9
  • 59
  • 108
0

Try changing:

@answers_count = Question.answers.count

to:

@answers_count = @question.answers.count

You are finding a particular question in your first line and assigning it to @question, but then are not querying that particular question (using Question instead of @question)

Also, look into the differences between count and size to make sure you are not querying the DB more than you have to -- this is a good discussion: ActiveRecord: size vs count

Community
  • 1
  • 1
Jim
  • 2,300
  • 1
  • 19
  • 43
  • Found it, i can call directly @question.answers.count. For some reason in the past this didnt worked and the aproach in my Question worked. Now it works simply like that. – Mini John Sep 25 '13 at 18:39