I guess this is a pretty basic thing, but I dug thru all the books I have and found nothing... Basically, I have a CMS system which has Post class as a parent to Page, NewsPage and EventsPage classes. I need to read all the recently updated pages, news and events, and get an array of "posts" of correct class. Here''s how it is implemented currently: pages_news_events = Post.find(:all, :conditions => ''post_type in ("page", "newspage", "events_page")'', :select => "id, post_type").collect do |onepage| case onepage.post_type when ''page'' Page.find_by_id(onepage.id) when ''newspage'' NewsPage.find_by_id(onepage.id) when ''events_page'' EventsPage.find_by_id(onepage.id) end end I basically just read the necessary minimum of "generic" Posts info from the database (id and type) and then "coerce" these records to full-fledged news/events/pages items depending on post_type column. What would be cool to have it the ability to get this arrays without additional find_by_ids so I won''t spawn lots of single-line queries. Coercion to child class or something. Unfortunately I was unable anything to achieve that. Or may be there''s some Rails magic I can use in this case? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 5 Nov 2007, at 07:12, Mikhail Kornienko wrote:> > I guess this is a pretty basic thing, but I dug thru all the books I > have and found nothing... > > Basically, I have a CMS system which has Post class as a parent to > Page, NewsPage and EventsPage classes. > I need to read all the recently updated pages, news and events, and > get an array of "posts" of correct class. > > Here''s how it is implemented currently: > > pages_news_events = Post.find(:all, :conditions => ''post_type in > ("page", "newspage", "events_page")'', :select => "id, > post_type").collect do |onepage| > case onepage.post_type > when ''page'' > Page.find_by_id(onepage.id) > when ''newspage'' > NewsPage.find_by_id(onepage.id) > when ''events_page'' > EventsPage.find_by_id(onepage.id) > end > end > > I basically just read the necessary minimum of "generic" Posts info > from the database (id and type) and then "coerce" these records to > full-fledged news/events/pages items depending on post_type column. > > What would be cool to have it the ability to get this arrays without > additional find_by_ids so I won''t spawn lots of single-line queries. > Coercion to child class or something. Unfortunately I was unable > anything to achieve that.So do you have separate pages, events_pages etc... tables ? If you could use single table inheritance then this would all go away. You could also have a polymorphic association: you have a page_type and page_id column, say belongs_to :page, :polymorphic => true. If you then do post.page then rails will essentially do the case statement you''ve got above. You can''t do an :include on a polymorphic belongs to though, although you can write some queries to the effect of ''get all page_ids where page_type = ''news_page'' and then load all NewsPages in one go with those ids. Fred --~--~---------~--~----~------------~-------~--~----~ 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 have all the pages stored in a single table, and distinct between them by "page_type" column. But I wonder how do I do that table inheritance you are suggesting? I currently have: class Post < ActiveRecord::Base class Page < Post class EventPage < Post etc.. Guess I''m just slow at the end of the day, but I can''t come up with any code right now :/ If you have a minute and it''s not too much of a hustle, could you show me a simple example of how you would do that? Would be much appreciated! Thanks, Mike. On Nov 5, 7:13 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5 Nov 2007, at 07:12, Mikhail Kornienko wrote: > > > > > > > I guess this is a pretty basic thing, but I dug thru all the books I > > have and found nothing... > > > Basically, I have a CMS system which has Post class as a parent to > > Page, NewsPage and EventsPage classes. > > I need to read all the recently updated pages, news and events, and > > get an array of "posts" of correct class. > > > Here''s how it is implemented currently: > > > pages_news_events = Post.find(:all, :conditions => ''post_type in > > ("page", "newspage", "events_page")'', :select => "id, > > post_type").collect do |onepage| > > case onepage.post_type > > when ''page'' > > Page.find_by_id(onepage.id) > > when ''newspage'' > > NewsPage.find_by_id(onepage.id) > > when ''events_page'' > > EventsPage.find_by_id(onepage.id) > > end > > end > > > I basically just read the necessary minimum of "generic" Posts info > > from the database (id and type) and then "coerce" these records to > > full-fledged news/events/pages items depending on post_type column. > > > What would be cool to have it the ability to get this arrays without > > additional find_by_ids so I won''t spawn lots of single-line queries. > > Coercion to child class or something. Unfortunately I was unable > > anything to achieve that. > > So do you have separate pages, events_pages etc... tables ? If you > could use single table inheritance then this would all go away. > > You could also have a polymorphic association: you have a page_type > and page_id column, say belongs_to :page, :polymorphic => true. > If you then do post.page then rails will essentially do the case > statement you''ve got above. You can''t do an :include on a polymorphic > belongs to though, although you can write some queries to the effect > of ''get all page_ids where page_type = ''news_page'' and then load all > NewsPages in one go with those ids. > > Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 5 Nov 2007, at 11:54, Mikhail Kornienko wrote:> > I have all the pages stored in a single table, and distinct between > them by "page_type" column. > But I wonder how do I do that table inheritance you are suggesting? > > I currently have: > class Post < ActiveRecord::Base > class Page < Post > class EventPage < Post > etc.. > > > Guess I''m just slow at the end of the day, but I can''t come up with > any code right now :/ If you have a minute and it''s not too much of a > hustle, could you show me a simple example of how you would do that? > Would be much appreciated! >if you have a type column, then rails will use that to instantiate the correct subclass when the row is loaded. Gred> Thanks, > Mike. > > On Nov 5, 7:13 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 5 Nov 2007, at 07:12, Mikhail Kornienko wrote: >> >> >> >> >> >>> I guess this is a pretty basic thing, but I dug thru all the books I >>> have and found nothing... >> >>> Basically, I have a CMS system which has Post class as a parent to >>> Page, NewsPage and EventsPage classes. >>> I need to read all the recently updated pages, news and events, and >>> get an array of "posts" of correct class. >> >>> Here''s how it is implemented currently: >> >>> pages_news_events = Post.find(:all, :conditions => ''post_type in >>> ("page", "newspage", "events_page")'', :select => "id, >>> post_type").collect do |onepage| >>> case onepage.post_type >>> when ''page'' >>> Page.find_by_id(onepage.id) >>> when ''newspage'' >>> NewsPage.find_by_id(onepage.id) >>> when ''events_page'' >>> EventsPage.find_by_id(onepage.id) >>> end >>> end >> >>> I basically just read the necessary minimum of "generic" Posts info >>> from the database (id and type) and then "coerce" these records to >>> full-fledged news/events/pages items depending on post_type column. >> >>> What would be cool to have it the ability to get this arrays without >>> additional find_by_ids so I won''t spawn lots of single-line queries. >>> Coercion to child class or something. Unfortunately I was unable >>> anything to achieve that. >> >> So do you have separate pages, events_pages etc... tables ? If you >> could use single table inheritance then this would all go away. >> >> You could also have a polymorphic association: you have a page_type >> and page_id column, say belongs_to :page, :polymorphic => true. >> If you then do post.page then rails will essentially do the case >> statement you''ve got above. You can''t do an :include on a polymorphic >> belongs to though, although you can write some queries to the effect >> of ''get all page_ids where page_type = ''news_page'' and then load all >> NewsPages in one go with those ids. >> >> Fred > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ah indeed! Thanks! :) Mike. On 05.11.2007, at 21:07, Frederick Cheung wrote:> > > On 5 Nov 2007, at 11:54, Mikhail Kornienko wrote: > >> >> I have all the pages stored in a single table, and distinct between >> them by "page_type" column. >> But I wonder how do I do that table inheritance you are suggesting? >> >> I currently have: >> class Post < ActiveRecord::Base >> class Page < Post >> class EventPage < Post >> etc.. >> >> >> Guess I''m just slow at the end of the day, but I can''t come up with >> any code right now :/ If you have a minute and it''s not too much of a >> hustle, could you show me a simple example of how you would do that? >> Would be much appreciated! >> > > if you have a type column, then rails will use that to instantiate the > correct subclass when the row is loaded. > > Gred > > >> Thanks, >> Mike. >> >> On Nov 5, 7:13 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >> wrote: >>> On 5 Nov 2007, at 07:12, Mikhail Kornienko wrote: >>> >>> >>> >>> >>> >>>> I guess this is a pretty basic thing, but I dug thru all the >>>> books I >>>> have and found nothing... >>> >>>> Basically, I have a CMS system which has Post class as a parent to >>>> Page, NewsPage and EventsPage classes. >>>> I need to read all the recently updated pages, news and events, and >>>> get an array of "posts" of correct class. >>> >>>> Here''s how it is implemented currently: >>> >>>> pages_news_events = Post.find(:all, :conditions => ''post_type in >>>> ("page", "newspage", "events_page")'', :select => "id, >>>> post_type").collect do |onepage| >>>> case onepage.post_type >>>> when ''page'' >>>> Page.find_by_id(onepage.id) >>>> when ''newspage'' >>>> NewsPage.find_by_id(onepage.id) >>>> when ''events_page'' >>>> EventsPage.find_by_id(onepage.id) >>>> end >>>> end >>> >>>> I basically just read the necessary minimum of "generic" Posts info >>>> from the database (id and type) and then "coerce" these records to >>>> full-fledged news/events/pages items depending on post_type column. >>> >>>> What would be cool to have it the ability to get this arrays >>>> without >>>> additional find_by_ids so I won''t spawn lots of single-line >>>> queries. >>>> Coercion to child class or something. Unfortunately I was unable >>>> anything to achieve that. >>> >>> So do you have separate pages, events_pages etc... tables ? If you >>> could use single table inheritance then this would all go away. >>> >>> You could also have a polymorphic association: you have a page_type >>> and page_id column, say belongs_to :page, :polymorphic => true. >>> If you then do post.page then rails will essentially do the case >>> statement you''ve got above. You can''t do an :include on a >>> polymorphic >>> belongs to though, although you can write some queries to the effect >>> of ''get all page_ids where page_type = ''news_page'' and then load all >>> NewsPages in one go with those ids. >>> >>> Fred >> >> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---