I have the following models: Category: has_many :sub_categories Sub_Category: belongs_to :category has_many :posts Post: belongs_to :sub_category I''m trying to build a page that will list Categories with Pjax tabs and then each Category tab will list all it''s Sub-Categories in widgets with 5 posts in each widget. I have an idea of how to loop through the data once I have it but I''m having trouble structuring the query. Thanks for any help. @category.sub_categories.each do |sub_cat| sub_cat.posts.each_slice(5) do |posts| #here you create a widget, posts have at most 5 elements thanks to each_slice method posts.each do |post| #do something with each post end endend -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/uIAGm7-cz9oJ. For more options, visit https://groups.google.com/groups/opt_out.
On 4 March 2013 15:35, Anthony DeFreitas <anthony-rd94rAj8PXNQDpKm1IfdgkEOCMrvLtNR@public.gmane.org> wrote:> I have the following models: > > Category: > has_many :sub_categories > > Sub_Category: > > belongs_to :category > has_many :posts > > Post: > belongs_to :sub_category > > I''m trying to build a page that will list Categories with Pjax tabs and then > each Category tab will list all it''s Sub-Categories in widgets with 5 posts > in each widget. > > I have an idea of how to loop through the data once I have it but I''m having > trouble structuring the query. Thanks for any help. > > @category.sub_categories.each do |sub_cat| > sub_cat.posts.each_slice(5) do |posts| > #here you create a widget, posts have at most 5 elements thanks to > each_slice method > posts.each do |post| > #do something with each post > end > end > endIt is not clear to me what question you are trying to ask here. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
My question is how do I structure a query that will return all the categories with the related sub-categories and the related posts. On Monday, March 4, 2013 5:34:42 PM UTC-4, Colin Law wrote:> > On 4 March 2013 15:35, Anthony DeFreitas <ant...-rd94rAj8PXNQDpKm1IfdgkEOCMrvLtNR@public.gmane.org<javascript:>> > wrote: > > I have the following models: > > > > Category: > > has_many :sub_categories > > > > Sub_Category: > > > > belongs_to :category > > has_many :posts > > > > Post: > > belongs_to :sub_category > > > > I''m trying to build a page that will list Categories with Pjax tabs and > then > > each Category tab will list all it''s Sub-Categories in widgets with 5 > posts > > in each widget. > > > > I have an idea of how to loop through the data once I have it but I''m > having > > trouble structuring the query. Thanks for any help. > > > > @category.sub_categories.each do |sub_cat| > > sub_cat.posts.each_slice(5) do |posts| > > #here you create a widget, posts have at most 5 elements thanks to > > each_slice method > > posts.each do |post| > > #do something with each post > > end > > end > > end > > It is not clear to me what question you are trying to ask here. > > Colin >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/vKd-JBNGsYYJ. For more options, visit https://groups.google.com/groups/opt_out.
On 5 March 2013 01:43, Anthony DeFreitas <anthony-rd94rAj8PXNQDpKm1IfdgkEOCMrvLtNR@public.gmane.org> wrote:> My question is how do I structure a query that will return all the > categories with the related sub-categories and the related posts.If you just get all the relevent categories in @categories then for each category accessed via something like @categories.each do |category| then you can use category.sub_categories and then for each of those the posts in exactly the way suggested in your original code. There is no need to do anything special in the query, just fetch the relevant categories. By the way could you not top post please, it makes it difficult to follow the thread. Insert you reply at appropriate points in the previous message. Thanks. Colin> > On Monday, March 4, 2013 5:34:42 PM UTC-4, Colin Law wrote: >> >> On 4 March 2013 15:35, Anthony DeFreitas <ant...-rd94rAj8PXNQDpKm1IfdgkEOCMrvLtNR@public.gmane.org> >> wrote: >> > I have the following models: >> > >> > Category: >> > has_many :sub_categories >> > >> > Sub_Category: >> > >> > belongs_to :category >> > has_many :posts >> > >> > Post: >> > belongs_to :sub_category >> > >> > I''m trying to build a page that will list Categories with Pjax tabs and >> > then >> > each Category tab will list all it''s Sub-Categories in widgets with 5 >> > posts >> > in each widget. >> > >> > I have an idea of how to loop through the data once I have it but I''m >> > having >> > trouble structuring the query. Thanks for any help. >> > >> > @category.sub_categories.each do |sub_cat| >> > sub_cat.posts.each_slice(5) do |posts| >> > #here you create a widget, posts have at most 5 elements thanks to >> > each_slice method >> > posts.each do |post| >> > #do something with each post >> > end >> > end >> > end >> >> It is not clear to me what question you are trying to ask here. >> >> Colin-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Tue, Mar 5, 2013 at 2:56 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 5 March 2013 01:43, Anthony DeFreitas <anthony-rd94rAj8PXNQDpKm1IfdgkEOCMrvLtNR@public.gmane.org> > wrote: > > My question is how do I structure a query that will return all the > > categories with the related sub-categories and the related posts. > > If you just get all the relevent categories in @categories then for > each category accessed via something like > @categories.each do |category| > then you can use category.sub_categories and then for each of those > the posts in exactly the way suggested in your original code. There > is no need to do anything special in the query, just fetch the > relevant categories. > > By the way could you not top post please, it makes it difficult to > follow the thread. Insert you reply at appropriate points in the > previous message. Thanks. > > Colin > > > > > On Monday, March 4, 2013 5:34:42 PM UTC-4, Colin Law wrote: > >> > >> On 4 March 2013 15:35, Anthony DeFreitas <ant...-rd94rAj8PXNQDpKm1IfdgkEOCMrvLtNR@public.gmane.org> > >> wrote: > >> > I have the following models: > >> > > >> > Category: > >> > has_many :sub_categories > >> > > >> > Sub_Category: > >> > > >> > belongs_to :category > >> > has_many :posts > >> > > >> > Post: > >> > belongs_to :sub_category > >> > > >> > I''m trying to build a page that will list Categories with Pjax tabs > and > >> > then > >> > each Category tab will list all it''s Sub-Categories in widgets with 5 > >> > posts > >> > in each widget. > >> > > >> > I have an idea of how to loop through the data once I have it but I''m > >> > having > >> > trouble structuring the query. Thanks for any help. > >> > >Do you mean to say you''ve got the view code but don''t know what should go in the controller?> >> > @category.sub_categories.each do |sub_cat| > >> > sub_cat.posts.each_slice(5) do |posts| > >> > #here you create a widget, posts have at most 5 elements thanks to > >> > each_slice method > >> > posts.each do |post| > >> > #do something with each post > >> > end > >> > end > >> > end > >> > >> It is not clear to me what question you are trying to ask here. > >> > >> Colin > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
You dont need to structure the query to the db. you get all categories, subcategories and posts with @categories = Category.all (or something like Category.where(:xy => z) You can iterate over the @categories like this: @categories.each do |category| # Do stuff for your category iteration here category.sub_categories.each do |sub| # Do stuff of sub category here sub.posts.each_slice(5) do |slice| # Slice stuff here (each 5 posts do something, like a div or something.) slice.each do |post| # do single post stuff here end end end end Am Dienstag, 5. März 2013 02:43:50 UTC+1 schrieb Anthony DeFreitas:> > My question is how do I structure a query that will return all the > categories with the related sub-categories and the related posts. > > On Monday, March 4, 2013 5:34:42 PM UTC-4, Colin Law wrote: >> >> On 4 March 2013 15:35, Anthony DeFreitas <ant...-rd94rAj8PXNQDpKm1IfdgkEOCMrvLtNR@public.gmane.org> >> wrote: >> > I have the following models: >> > >> > Category: >> > has_many :sub_categories >> > >> > Sub_Category: >> > >> > belongs_to :category >> > has_many :posts >> > >> > Post: >> > belongs_to :sub_category >> > >> > I''m trying to build a page that will list Categories with Pjax tabs and >> then >> > each Category tab will list all it''s Sub-Categories in widgets with 5 >> posts >> > in each widget. >> > >> > I have an idea of how to loop through the data once I have it but I''m >> having >> > trouble structuring the query. Thanks for any help. >> > >> > @category.sub_categories.each do |sub_cat| >> > sub_cat.posts.each_slice(5) do |posts| >> > #here you create a widget, posts have at most 5 elements thanks to >> > each_slice method >> > posts.each do |post| >> > #do something with each post >> > end >> > end >> > end >> >> It is not clear to me what question you are trying to ask here. >> >> Colin >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Af0irRumVVYJ. For more options, visit https://groups.google.com/groups/opt_out.