Matteo
Matteo

Reputation: 1136

Showing a count of associated records

As part of my site admin, I need to occasionally update project categories to reflect changing user requirements.

in my show method (CategoriesController) , I have the following code:

@projects = Project.count.where(:project.category_id => :id)

My controller looks like this:

class ProjectCategory < ActiveRecord::Base
  attr_accessible :project_category
  belongs_to :project
end

I can't seem to get it to populate the count of all projects for each category. Any help?

Upvotes: 1

Views: 38

Answers (1)

alestanis
alestanis

Reputation: 21863

You should change the line

@projects = Project.count.where(:project.category_id => :id)

to

@projects = Project.where(:project.category_id => :id).count

Upvotes: 2

Related Questions