{"id":23,"date":"2008-10-03T16:30:43","date_gmt":"2008-10-03T20:30:43","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/lianaleahy\/?p=23"},"modified":"2008-10-03T16:30:55","modified_gmt":"2008-10-03T20:30:55","slug":"nested-sets-and-scope","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/2008\/10\/03\/nested-sets-and-scope\/","title":{"rendered":"Nested Sets and Scope"},"content":{"rendered":"<p>A colleague and I both had this same issue working with BNS and since there wasn&#8217;t much about it on the net, I figured I&#8217;d share.<\/p>\n<p>Each <a href=\"en.wikipedia.org\/wiki\/Top-level_domain\">TLD<\/a> in my nested set will be its own tree root.  And multiple tree roots in a nested set require a scope.<\/p>\n<p><code><br \/>\nacts_as_nested_set :scope =&gt; :root<br \/>\n<\/code><\/p>\n<p>The <a href=\"http:\/\/wiki.rubyonrails.org\/rails\/pages\/BetterNestedSet\">ruby on rails wiki<\/a> mentions scope using a root_id column, but you&#8217;ll notice in the example that the root records have a root_id of nil.  <b>This just doesn&#8217;t work.<\/b><\/p>\n<p>When you attempt to <b>move_to_child_of<\/b> your new node record after create, you&#8217;ll recieve this error:<\/p>\n<pre>\r\nImpossible move, target node cannot be inside moved tree.\r\n<\/pre>\n<p>Now, we couldn&#8217;t find any documentation anywhere but after much trial and error my colleague discovered that populating the root_id of a root record with its own id, resolves the issue.<\/p>\n<p><code><br \/>\n       root = Domain.new<br \/>\n       subdomains.each_with_index do |subdomain, index|<br \/>\n          node = Domain.find_by_subdomain_and_parent_id(subdomain, parent.id)<br \/>\n          if node.nil?<br \/>\n            node = Domain.create(:subdomain =&gt; subdomain, root_id =&gt; root.id)<br \/>\n            if parent.new_record? #is this a root record?<br \/>\n              node.root_id = node.id  # &lt;-------------------------------- populate root for root records<br \/>\n              node.save<br \/>\n              root = node<br \/>\n            else<br \/>\n              node.move_to_child_of parent.id<br \/>\n            end<br \/>\n          end<br \/>\n          parent = node<br \/>\n        end<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A colleague and I both had this same issue working with BNS and since there wasn&#8217;t much about it on the net, I figured I&#8217;d share. Each TLD in my nested set will be its own tree root. And multiple tree roots in a nested set require a scope. acts_as_nested_set :scope =&gt; :root The ruby [&hellip;]<\/p>\n","protected":false},"author":1911,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2403],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-ruby-on-rails"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/users\/1911"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":0,"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/lianaleahy\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}