def self.categories_with_count
Category.find_by_sql("SELECT categories.name AS name, categories.slug AS slug, COUNT(categorizations.id) AS count FROM categories LEFT OUTER JOIN categorizations ON categories.id = categorizations.category_id GROUP BY categorizations.category_id, categories.name, categories.slug ORDER BY categories.name")
end
Refactorings
No refactoring yet !
Rick M
May 24, 2011, May 24, 2011 20:07, permalink
Use a counter cache: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to
# First run: script/generate migration AddCategorizationsCountToCategories categorizations_count:integer class Category < ActiveRecord::Base has_many :categorizations, :order => 'name' end class Categorization < ActiveRecord::Base belongs_to :category, :counter_cache => true end
Rick M
May 24, 2011, May 24, 2011 20:07, permalink
Use a counter cache: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to
# First run: script/generate migration AddCategorizationsCountToCategories categorizations_count:integer class Category < ActiveRecord::Base has_many :categorizations, :order => 'name' end class Categorization < ActiveRecord::Base belongs_to :category, :counter_cache => true end
Elhu
May 25, 2011, May 25, 2011 02:00, permalink
Will it work the same if I have a Many to many relationship? Since it's the case for me, I'd have three models classes as follow:
class Category < ActiveRecord::Base has_many :blogs, :through => :categorizations has_many :categorizations, :order => 'name' #by the way, the name column is in Category, will this work? end class Categorization < ActiveRecord::Base belongs_to :category, :counter_cache => true belongs_to :blog end class Blog << ActiveRecord::Base has_many :categories, :through => :categorizations has_many :categorizations end
Elhu
May 25, 2011, May 25, 2011 02:00, permalink
Will it work the same if I have a Many to many relationship? Since it's the case for me, I'd have three models classes as follow:
class Category < ActiveRecord::Base has_many :blogs, :through => :categorizations has_many :categorizations, :order => 'name' #by the way, the name column is in Category, will this work? end class Categorization < ActiveRecord::Base belongs_to :category, :counter_cache => true belongs_to :blog end class Blog << ActiveRecord::Base has_many :categories, :through => :categorizations has_many :categorizations end
Aimee
July 26, 2011, July 26, 2011 02:18, permalink
Vous avez de bons points il, c'est pourquoi j'aime toujours verifier votre blog, Il semble que vous etes un expert dans ce domaine. maintenir le bon travail, Mon ami recommander votre blog.
Mon francais n'est pas tres bon, je suis de l'Allemagne.
Mon blog:
<a href="http://fr.wikipedia.org/wiki/Crédit_immobilier">Credit immobilier</a> et <a href="http://www.rachatdecredit.net">rachat de credit cliquez ici</a>
Hi!
I'm trying to retrieve from the database the name of all categories and for each the number of blog entries with this category. This code does work but I wonder if there is a way to do this more nicely using the "Rails way". Category is an ActiveRecord class.
PS: the category slug is needed to generate the category URL in the view.