55bb5ae89ab81b436969697f65b60533

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.

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 !

Debde181b3a5925bb347193b7568de6d

Rick M

May 24, 2011, May 24, 2011 20:07, permalink

No rating. Login to rate!
# 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
Debde181b3a5925bb347193b7568de6d

Rick M

May 24, 2011, May 24, 2011 20:07, permalink

No rating. Login to rate!
# 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
55bb5ae89ab81b436969697f65b60533

Elhu

May 25, 2011, May 25, 2011 02:00, permalink

No rating. Login to rate!

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
55bb5ae89ab81b436969697f65b60533

Elhu

May 25, 2011, May 25, 2011 02:00, permalink

No rating. Login to rate!

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
0c949162a183b56f8c1d9682ebb6c066

Aimee

July 26, 2011, July 26, 2011 02:18, permalink

No rating. Login to rate!

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>

Your refactoring





Format Copy from initial code

or Cancel