Let''s say I''ve got a forum app where there are Topic objects and Post objects. Each Post has a topic_id column and a parent_id column. Each topic has a root_post that, minus error-checking, etc., boils down to: def root_post Post.find( :first, :conditions => "topic_id = #{self.id} and parent_id IS NULL" ) end Where can I store the root_post so that I don''t have to go to the DB every time I need it? This just *has* to be brain dead simple, but I haven''t found the answer yet. If I put any ActiveRecord into the session, I get marshalling problems when the session is saved to the DB. Perhaps there is a place to store things in the session that *don''t* get saved? Or a more general app-level caching mechanism? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
This won''t really solve your problem, but you could simplify your root_post method as a ''has_one'' association with an order specified: has_one :root_post, :class_name => ''Post'', :order => ''posted_at DESC'', :conditions => ''parent_id IS NULL'' This will only load once per model I think - but I am not 100% certain. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> This will only load once per model I think - but I am not 100% certain.I meant ''once per instance''... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Dodwell wrote:> This won''t really solve your problem, but you could simplify your > root_post method as a ''has_one'' association with an order specified: > > has_one :root_post, :class_name => ''Post'', :order => ''posted_at DESC'', > :conditions => ''parent_id IS NULL'' > > This will only load once per model I think - but I am not 100% certain.Wouldn''t the condition have to include thread_id? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Robert Head wrote:> > Wouldn''t the condition have to include thread_id?Or is that implied in has_one? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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''m not sure how the Thread model fits into your system. Assuming that you have two models:, you can simply do: -- class Topic < ActiveRecord::Base has_many :posts has_one :root_post, :class_name => ''Post'', :conditions => ''parent_id IS NULL'' has_many :replies, :class_name => ''Post'', :offset => 2 end class Post < ActiveRecord::Base belongs_to :topic end -- All that is needed to link these up is a ''topic_id'' fk in your post table. ~ Mark Dodwell -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Dodwell wrote:> I''m not sure how the Thread model fits into your system. Assuming that > you have two models:, you can simply do: > > -- > > class Topic < ActiveRecord::Base > > has_many :posts > has_one :root_post, :class_name => ''Post'', :conditions => ''parent_id > IS NULL'' > has_many :replies, :class_name => ''Post'', :offset => 2 > > end > > > class Post < ActiveRecord::Base > > belongs_to :topic > > end > > -- > > All that is needed to link these up is a ''topic_id'' fk in your post > table. > > > ~ Mark DodwellThanks. By ''Thread'', I meant ''Topic''. I''m still curious about the more general case. How do I store ActiveRecords in the Session without marshalling problems? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> If I put any ActiveRecord into the session, I get marshalling problems > when the session is saved to the DB. Perhaps there is a place to store > things in the session that *don''t* get saved? Or a more general > app-level caching mechanism?The only built-in caching deals with caching the rendered output. Folks tend to use something like memcache to cache the actual models though. Look at acts_as_cached and cached_model: acts_as_cached http://errtheblog.com/post/27 cached_model http://dev.robotcoop.com/Libraries/ -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---