Forum: Ruby on Rails Help needed with acts_as_list

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-26 12:21
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
6661ef9d747db3af8896cd94959d717d?d=identicon&s=25 Paul Barry (Guest)
on 2006-05-26 14:21
(Received via mailing list)
Just do acts_as_list :scope => :in_exhibition
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-26 17:18
Paul Barry 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.
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-26 17:18
Nuno wrote:
>
> except

Sorry, I mean expect !
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-05-26 18:49
(Received via mailing list)
On 5/26/06, Nuno <nomail@novalidatall991.com> 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.
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-27 09:43
Wilson Bilkovich 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
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-05-27 23:49
(Received via mailing list)
On 5/27/06, Nuno <nomail@novalidatall991.com> wrote:
> Wilson Bilkovich 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.
This topic is locked and can not be replied to.