# Acts\_as\_list with 2 fields in the scope

**URL:** <https://www.ruby-forum.com/t/acts-as-list-with-2-fields-in-the-scope/48846>\
**Category:** Rails\
**Created:** [November 25, 2005, 4:10pm UTC](https://www.ruby-forum.com/t/acts-as-list-with-2-fields-in-the-scope/48846 "2005-11-25T16:10:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [November 25, 2005, 4:10pm UTC](https://www.ruby-forum.com/t/acts-as-list-with-2-fields-in-the-scope/48846/1 "2005-11-25T16:10:40Z")

</div>

Hi Railers,

I’ve got a Categories table.  
I want it to act as a list within the scope of the parent\_id AND the  
site\_id.

Categories table :  
id  
label  
site\_id  
parent\_id

So, in my Category class, I have :  
acts\_as\_list :scope =\> ‘site\_id = #{site\_id} AND parent\_id = #  
{parent\_id}’

The problem is that when I try to move\_up a Category with a parent\_id  
that is null, the generated query (found in my development.log) is  
“SELECT \* FROM categories WHERE (site\_id = 1 AND parent\_id = AND  
position = 3)” which gives me an error (because of “parent\_id = AND”).

So, my question is “how can I solve this problem?”.

Many thanks in advance for you help that will be much appreciated!

Thomas B…

P.S. : if I only set the scope to parent\_id, I have no problem, the  
generated query is “SELECT \* FROM categories WHERE (parent\_id IS NULL)”

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [November 28, 2005, 9:21pm UTC](https://www.ruby-forum.com/t/acts-as-list-with-2-fields-in-the-scope/48846/2 "2005-11-28T21:21:37Z")

</div>

Thomas,

This happens because the code for acts\_as\_list behaves differently  
when the scope is a symbol versus a string like the one you’re using.  
When the scope is a symbol, acts\_as\_list knows how to check for null  
and generate “IS NULL” as the condition. When you pass a string as  
the scope, that string is used verbatim as the condition, with  
runtime substitution of the values of any interpolated variables (the  
stuff inside the #{…}).

Perhaps it would help to look at the source code at http://  
[ar.rubyonrails.com/classes/ActiveRecord/Acts/List/](http://ar.rubyonrails.com/classes/ActiveRecord/Acts/List/)  
ClassMethods.html#M000022. Be sure to click on the “Show Source” link  
at the bottom of the acts\_as\_list entry, if it isn’t already showing.

Line 35 sets up some default key / value pairs. These will be  
augmented (and possibly overridden) at line 36 by any that you pass  
in, like :scope.

If you pass in “:scope =\> :some\_symbol”, lines 41 - 49 define a  
method called scope\_condition() that looks like this:

def scope\_condition  
if parent\_id.nil?  
“parent\_id IS NULL”  
else  
“parent\_id = #{parent\_id}”  
end  
end

and behaves as you expect when the runtime value of the symbol is nil.

If your :scope value is anything but a symbol (a string, in your  
case), line 51 is executed instead. It generates a method that looks  
like:

def scope\_condition()  
“site\_id = #{site\_id} AND parent\_id = #{parent\_id}”  
end

Unfortunately, all that happens here when parent\_id is nil is what  
you saw in your log; i.e., it is interpolated as no character. You  
might expect it to create something like:

site\_id = 123 AND parent\_id = nil

at runtime, but even that wouldn’t be helpful. You can see a  
difference in how nil is handled by opening an irb session and  
entering “puts nil”, followed by “puts #{nil}”.

If it’s any consolation, the example used in the acts\_as\_list  
documentation (“acts\_as\_list :scope =\> ?todo\_list\_id = #  
{todo\_list\_id} AND completed = 0?”) will fail in the same way that  
yours does if todo\_list\_id is nil.

Hope this helps,  
David

---

<div class="post-metadata">

**Author:** ![thomas.tmp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@thomas.tmp](https://www.ruby-forum.com/u/thomas.tmp)\
**Post date:** [December 1, 2005, 3:48pm UTC](https://www.ruby-forum.com/t/acts-as-list-with-2-fields-in-the-scope/48846/3 "2005-12-01T15:48:46Z")

</div>

Hello David,

Thank you for your help!  
I noticed the same thing, and I submitted a bug report regarding this.  
You can read it here : [http://dev.rubyonrails.org/ticket/3018](http://dev.rubyonrails.org/ticket/3018).  
It was posted 6 days ago, but is still not being managed.

Kind regards,  
Thomas.
