# Help needed with acts\_as\_list

**URL:** <https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709>\
**Category:** Rails\
**Created:** [May 26, 2006, 12:21pm UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709 "2006-05-26T12:21:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 26, 2006, 12:21pm UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709/1 "2006-05-26T12:21:01Z")

</div>

Hi ! There must be something I don’t understand clearly in acts\_as\_list

I have a list of gallery entries (images + text), model is GalleryEntry.  
I have a field in\_exhibition (boolean)

I want the list to be sorted with a scope on in\_exhibition : from 1 to N  
for every record having in\_exhibition = false and 1 to N for every  
record having in\_exhibition = true

When I use acts\_as\_list :scope =\> “in\_exhibition = #{in\_exhibition}”

I get a runtime error :  
undefined local variable or method `in\_exhibition’ for  
GalleryEntry:Class

Is it possible to use acts\_as\_list that way ??

If not what is the solution ? Creating another model GalleryEntryType  
and :  
GalleryEntry  
belongs\_to :gallery\_entry\_type  
acts\_as\_list :scope =\> :gallery\_entry\_type

??

Any help appreciated I don’t know what’s wrong or right… thank you

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 26, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709/2 "2006-05-26T14:21:01Z")

</div>

Just do acts\_as\_list :scope =\> :in\_exhibition

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 26, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709/3 "2006-05-26T17:18:12Z")

</div>

Paul B. wrote:

> Just do acts\_as\_list :scope =\> :in\_exhibition

No, in that case RoR except a in\_exhibition\_id key field which handle  
the relation with a parent model.

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 26, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709/4 "2006-05-26T17:18:51Z")

</div>

Nuno wrote:

> except

Sorry, I mean expect !

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 26, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709/5 "2006-05-26T18:49:53Z")

</div>

On 5/26/06, Nuno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> acts\_as\_list :scope =\> :gallery\_entry\_type

You need to surround your ‘scope’ clause with single-quotes, rather  
than double-quotes.  
It’s a little funny, because you don’t want Ruby to evaluate  
#{in\_exhibition} when it is first seen in the class body. You only  
want that to happen when the queries execute.

acts\_as\_list :scope =\> ‘in\_exhibition = #{in\_exhibition}’  
should work.

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 27, 2006, 9:43am UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709/6 "2006-05-27T09:43:28Z")

</div>

Wilson B. wrote:

> acts\_as\_list :scope =\> ‘in\_exhibition = #{in\_exhibition}’  
> should work.

It does ! Thanks !

Please, could you explain a little what’s happening behind the scene ?  
What is is the difference between the ‘’ and the “” version ?

Thanks

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 27, 2006, 11:49pm UTC](https://www.ruby-forum.com/t/help-needed-with-acts-as-list/61709/7 "2006-05-27T23:49:58Z")

</div>

On 5/27/06, Nuno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Wilson B. wrote:
> 
> > acts\_as\_list :scope =\> ‘in\_exhibition = #{in\_exhibition}’  
> > should work.
> 
> It does ! Thanks !
> 
> Please, could you explain a little what’s happening behind the scene ?  
> What is is the difference between the ‘’ and the “” version ?

When Ruby sees a double-quoted string during execution, it immediately  
tries to ‘interpolate’ it.

In this case, that would mean looking for a local variable or method  
called in\_exhibition, and inserting in\_exhibition.to\_s into the  
string.

However, acts\_as\_list is a class method of ActiveRecord, and  
in\_exhibition is an INSTANCE method. When acts\_as\_list is executed,  
in\_exhibition is out of scope, and can’t be found. This makes sense,  
because what you’re saying is that the whole class acts\_as\_list, not  
just one instance of it.

The single-quote trick is a workaround for that, because it lets  
acts\_as\_list wait until later to hunt up the value of in\_exhibition.

Fairly weird, and personally I’d prefer the :scope option to work the  
same way as “validates\_uniqueness\_of”  
e.g.  
acts\_as\_list :scope =\> [:some\_column, :some\_other\_column]

Until someone writes a plugin for that, we’re stuck with the funny  
single-quoted string.  
You should also be able to write the above as:  
acts\_as\_list :scope =\> %q[in\_exhibition = #{in\_exhibition}]  
…if that ever makes it easier.
