<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="place"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="City"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:"Trebuchet MS";
        panose-1:2 11 6 3 2 2 2 2 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:purple;
        text-decoration:underline;}
code
        {font-family:"Courier New";}
pre
        {margin:0in;
        margin-bottom:.0001pt;
        background:#EEEEEE;
        font-size:8.5pt;
        font-family:"Courier New";}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:Arial;
        color:windowtext;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
        {page:Section1;}
-->
</style>

</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'>Hi folks,<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'>I&#8217;ve been working on an application
that has separate notions of users and people, where:<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'>user belongs_to :person<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'>and<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'>person acts_as_nested_set (using BNS, of
course)<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'>When Person is defined with (among other
things):<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'>&nbsp; belongs_to :organization<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>&nbsp; acts_as_nested_set :scope
=&gt; :organization<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>and I call Person.roots, I
get this lovely error (which is very similar to the error I get if I call
Person.destroy while using scoping):<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<pre style='line-height:13.5pt'><code><font size=2 color="#333333"
face="Courier New"><span style='font-size:10.0pt;color:#333333'>Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? ? &quot;organization_id IS NULL&quot; : &quot;organization_id = #{organization_id}&quot; AND paren' at line 1: SELECT * FROM people WHERE ((organization_id.nil? ? &quot;organization_id IS NULL&quot; : &quot;organization_id = #{organization_id}&quot; AND parent_id IS NULL))&nbsp; ORDER BY lft</span></font></code><font
color="#333333"><span style='color:#333333'><o:p></o:p></span></font></pre>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>Which is accompanied by
this stack trace:<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<pre style='line-height:13.5pt'><code><font size=2 color="#333333"
face="Courier New"><span style='font-size:10.0pt;color:#333333'>K:/Rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract_adapter.rb:128:in `log'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333" face="Courier New"><span
style='font-size:10.0pt;color:#333333'>K:/Rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:243:in `execute'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333" face="Courier New"><span
style='font-size:10.0pt;color:#333333'>K:/Rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:399:in `select'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333" face="Courier New"><span
style='font-size:10.0pt;color:#333333'>K:/Rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333" face="Courier New"><span
style='font-size:10.0pt;color:#333333'>K:/Rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:427:in `find_by_sql'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333" face="Courier New"><span
style='font-size:10.0pt;color:#333333'>K:/Rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:997:in `find_every'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333" face="Courier New"><span
style='font-size:10.0pt;color:#333333'>K:/Rails/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:418:in `find'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333" face="Courier New"><span
style='font-size:10.0pt;color:#333333'>#{RAILS_ROOT}/vendor/plugins/better_nested_set/lib/better_nested_set.rb:128:in `</span></font></code><code><font
size=2 color="#333333" face="Trebuchet MS"><span style='font-size:10.0pt;
font-family:"Trebuchet MS";color:#333333'>roots'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333"
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:#333333'>#{RAILS_ROOT}/app/helpers/application_helper.rb:5:in `build_nested_tree'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333"
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:#333333'>#{RAILS_ROOT}/app/views/system/_users_box.rhtml:9:in `_run_rhtml_47app47views47system47_users_box46rhtml'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333"
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:#333333'>#{RAILS_ROOT}/app/views/system/manageusers.rhtml:1:in `_run_rhtml_47app47views47system47manageusers46rhtml'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333"
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:#333333'>-e:4:in `load'<o:p></o:p></span></font></code></pre><pre
style='line-height:13.5pt'><code><font size=2 color="#333333"
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:#333333'>-e:4</span></font></code><font color="#333333"
face="Trebuchet MS"><span style='font-family:"Trebuchet MS";color:#333333'><o:p></o:p></span></font></pre>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><st1:City w:st="on"><st1:place
 w:st="on"><font size=2 face="Trebuchet MS"><span style='font-size:10.0pt;
  font-family:"Trebuchet MS"'>Houston</span></font></st1:place></st1:City><font
size=2 face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS"'>,
we have a problem!&nbsp; Does anyone have any ideas?&nbsp; For now, I can turn
off the scoping during development, but scoping is a necessity for production.&nbsp;
I&#8217;ve tried monkeying around with it a bit, and I&#8217;ve been able to
get a bit closer, but I still wind up with an error on a SELECT statement that
looks like this:<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 color=black
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:black'>Mysql::Error: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax to
use near '' at line 1: SELECT * FROM people WHERE ((organization_id =
#{organization_id} AND parent_id IS NULL))&nbsp; ORDER BY lft<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 color=black
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:black'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 color=black
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:black'>This occurs when I change this line (line 79 in better_nested_set.rb)
from A to B:<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 color=black
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:black'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 color=black
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS";
color:black'>A:<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-indent:.5in;text-autospace:none'><font size=2
face="Trebuchet MS"><span style='font-size:10.0pt;font-family:"Trebuchet MS"'>options[:scope]
= %(#{options[:scope].to_s}.nil? ? &quot;#{options[:scope].to_s} IS NULL&quot;
: &quot;#{options[:scope].to_s} = \#{#{options[:scope].to_s}}&quot;)<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>B:<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
if</span></font><font size=2 face="Trebuchet MS"><span style='font-size:10.0pt;
font-family:"Trebuchet MS"'> %(#{options[:scope].to_s}).nil?<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options[:scope]
= &quot;#{options[:scope].to_s} IS NULL&quot;<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
else</span></font><font size=2 face="Trebuchet MS"><span style='font-size:10.0pt;
font-family:"Trebuchet MS"'><o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options[:scope]
= &quot;#{options[:scope].to_s} = \#{#{options[:scope].to_s}}&quot;<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
end</span></font><font size=2 face="Trebuchet MS"><span style='font-size:10.0pt;
font-family:"Trebuchet MS"'><o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>The problem seems to be
that the right side of the quoted comparison in the else portion of the above
block doesn&#8217;t properly evaluate to organization_id in situ.&nbsp; I&#8217;ve
tried a few different things to get it to work properly, but I can&#8217;t seem
to figure out where it&#8217;d get a value for organization_id (i.e. the scope field).&nbsp;
I&#8217;ll look at this again tomorrow, with fresh eyes, but so far, I&#8217;m
at my wit&#8217;s end.&nbsp; At least I&#8217;ve made some progress, including
trying to get the root and roots methods to evaluate the string (as that&#8217;s
where I expect it should be happening), I just can&#8217;t seem to figure out
how to make it fill in the blanks.<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>Any advice would be
welcome!&nbsp; I&#8217;d love to see a patch get committed, as it&#8217;ll make
BNS better, and it&#8217;ll make my application work properly in production.<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'><o:p>&nbsp;</o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>Thanks,<o:p></o:p></span></font></p>

<p class=MsoNormal style='text-autospace:none'><font size=2 face="Trebuchet MS"><span
style='font-size:10.0pt;font-family:"Trebuchet MS"'>-Jon Mischo<o:p></o:p></span></font></p>

</div>

</body>

</html>