Hi all, I''m doing something you all probably have done 100 times.. I''ve a cms type system with nested pages eg. http://www.example.com/about_us/mission_statement http://www.example.com/our_products/myshinything http://www.example.com/our_products/myshinything/large My Page model acts_as_nested_set, and has an attribute slug, in the above example we have a page with the slug "mission_statement" with parent page with an attribute "about_us". I need to look up Pages by the full path e.g. Page.find_by_path("about_us/mission_statement"), what is the best way to deal with that ? Can I save the ancestors.slugs / fullpath saved in the database somehow? Or other suggestions.. Hope that makes some sense ??? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/betternestedset-talk/attachments/20071016/6b495da1/attachment.html
Jean-Christophe Michel
2007-Oct-16 22:38 UTC
[Betternestedset-talk] hierarcical menu, caching
HI, Le 17 oct. 07 ? 00:10, Justin MacCarthy a ?crit :> Hi all, I''m doing something you all probably have done 100 times.. > > I''ve a cms type system with nested pages > > eg. > > http://www.example.com/about_us/mission_statement > http://www.example.com/our_products/myshinything > http://www.example.com/our_products/myshinything/large > > My Page model acts_as_nested_set, and has an attribute slug, in the > above example we have a page with the slug "mission_statement" > with parent page with an attribute "about_us". I need to look up > Pages by the full path e.g. Page.find_by_path("about_us/ > mission_statement"), what is the best way to deal with that ? Can I > save the ancestors.slugs / fullpath saved in the database somehow? > Or other suggestions..Here is how I deal with paths: in my controller I have a dispatch method that I get called by my route. The idea here is simply to load pages one by one, begining with the first url segment. If a segment is not a good page ''slug'' as you call it (it''s page.url for me) I default to the portion of the path already processed. Given the fact that most of times url are not more than 3 or 4 ''slugs'' deep, and that rails + db can cache repetitive similar requests, I think that the overload is worth the simplicity. def dispatch if params[:url].nil? or params[:url].empty? url = [] else url = params[:url].map{|l| l.gsub(/[^a-z0-9_\- \.]/,'''')}.delete_if{|l| l.empty?} end @page = Page.root if @page.nil? render :text => "<h1>404</h1><h2>No such page.</h2>", :layout => true, :status => 404 return true end url.each { |u| next_page = Page.find_by_url(u, :conditions => [''parent_id = ?'', @page.id]) if next_page.nil? break end @page = next_page } end Hope it helps ! Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/betternestedset-talk/attachments/20071017/e5fdc80b/attachment.html