<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> </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’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> </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> </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> </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> </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> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face="Trebuchet MS"><span style='font-size:
10.0pt;font-family:"Trebuchet MS"'> 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"'> acts_as_nested_set :scope
=> :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> </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> </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 '? ? "organization_id IS NULL" : "organization_id = #{organization_id}" AND paren' at line 1: SELECT * FROM people WHERE ((organization_id.nil? ? "organization_id IS NULL" : "organization_id = #{organization_id}" AND parent_id IS NULL)) 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> </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> </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> </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! Does anyone have any ideas? For now, I can turn
off the scoping during development, but scoping is a necessity for production.
I’ve tried monkeying around with it a bit, and I’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> </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)) 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> </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> </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> </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? ? "#{options[:scope].to_s} IS NULL"
: "#{options[:scope].to_s} = \#{#{options[:scope].to_s}}")<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> </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> </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"'>
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"'>
options[:scope]
= "#{options[:scope].to_s} IS NULL"<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"'>
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"'>
options[:scope]
= "#{options[:scope].to_s} = \#{#{options[:scope].to_s}}"<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"'>
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> </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’t properly evaluate to organization_id in situ. I’ve
tried a few different things to get it to work properly, but I can’t seem
to figure out where it’d get a value for organization_id (i.e. the scope field).
I’ll look at this again tomorrow, with fresh eyes, but so far, I’m
at my wit’s end. At least I’ve made some progress, including
trying to get the root and roots methods to evaluate the string (as that’s
where I expect it should be happening), I just can’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> </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! I’d love to see a patch get committed, as it’ll make
BNS better, and it’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> </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>