I understand how STI works, in that I have say a Post model that contains posts on a forum and several sub-posts like ''ordinaryUserPost'' and ''adminUserPost'' etc. Now, I want to define the same method in each of the sub-posts, but the method would do something different in each case, eg class Post < ActiveRecord::Base end class AdminUserPost < Post def background_color ''rockstar red'' end end class ordinaryUserPost < Post def background_color ''pale blue'' end end (yes its a silly example). Now in my thread controller I do Post.find (:all) and it gives me a list of posts I need to render, but they are ''Post'' objects, not AdminUserPost or OrdinaryUserPost - so I cannot just get my background_color method! I would have to do a find on each type of user post separately ... Is there anyway I can do: Post.find(:all) And in the resultant array get a list of AdminUserPost and OrdinaryUserPost objects instead of Post objects? Thanks, Stephen. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Apr-08 22:41 UTC
Re: Single table inheritance and find(:all) in parent
On Apr 8, 8:37 pm, "stephen O''D" <stephen.odonn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there anyway I can do: > > Post.find(:all) > > And in the resultant array get a list of AdminUserPost and > OrdinaryUserPost objects instead of Post objects? >That''s the way it should work out of the box. Would be interesting to see the data / code that results in this not happening. Fred> Thanks, > > Stephen.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > That''s the way it should work out of the box. Would be interesting to > see the data / code that results in this not happening.I was being stupid - put my type column was actually called post_type, and I added self.inheritance_column = ''''post_type'' in all the child classes, but forgot the parent! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---