Help needed with acts_as_list


#1

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


#2

Just do acts_as_list :scope => :in_exhibition


#3

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.


#4

Nuno wrote:

except

Sorry, I mean expect !


#5

On 5/26/06, Nuno 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.


#6

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


#7

On 5/27/06, Nuno 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.