So I have a Post model, and a Category model. I can''t wrap my head around trying to find the number of posts inside each category. I believe you would use a join for this, or some sort of association. The "models/category.rb": class Category < ActiveRecord::Base has_and_belongs_to_many :posts end The "models/post.rb" class Post < ActiveRecord::Base has_many :comments belongs_to :category end How would I do something like this? I assume you would query it like: @category = Category.find(:all) @category.post.size --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
GarrettB wrote:> So I have a Post model, and a Category model. > > I can''t wrap my head around trying to find the number of posts inside > each category. I believe you would use a join for this, or some sort > of association. > > The "models/category.rb": > > class Category < ActiveRecord::Base > has_and_belongs_to_many :posts > end > > The "models/post.rb" > class Post < ActiveRecord::Base > has_many :comments > belongs_to :category > end > > > How would I do something like this? I assume you would query it like:categor = Category.find(:all)> @category.post.size > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
GarrettB wrote:> class Category < ActiveRecord::Base > has_and_belongs_to_many :posts > end > > The "models/post.rb" > class Post < ActiveRecord::Base > has_many :comments > belongs_to :categoryBoth sides of a habtm are always habtm: has_and_belongs_to_many :categories> end > > How would I do something like this? I assume you would query it like:No @ - there''s no reason to make categories into an instance variable. categories = Category.find(:all) categories.each do |cat| p cat.posts.count end Think of cat.posts as simultaneously an array of all posts, and a super-model that can do anything Post can (such as Post.find()) but with the link to the Category automatically turned on. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I can''t seem to for the life of me to get this working, do you have an example of how my database should look? And how my associations need to be? On May 24, 2008, at 10:39 PM, Phlip wrote:> > GarrettB wrote: > >> class Category < ActiveRecord::Base >> has_and_belongs_to_many :posts >> end >> >> The "models/post.rb" >> class Post < ActiveRecord::Base >> has_many :comments >> belongs_to :category > > Both sides of a habtm are always habtm: > > has_and_belongs_to_many :categories > >> end >> >> How would I do something like this? I assume you would query it like: > > No @ - there''s no reason to make categories into an instance variable. > > categories = Category.find(:all) > categories.each do |cat| > p cat.posts.count > end > > Think of cat.posts as simultaneously an array of all posts, and a > super-model > that can do anything Post can (such as Post.find()) but with the > link to the > Category automatically turned on. > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---