# Attachment\_fu and acts\_as\_list - a land war in Asia

**URL:** <https://www.ruby-forum.com/t/attachment-fu-and-acts-as-list-a-land-war-in-asia/149541>\
**Category:** Rails\
**Created:** [September 26, 2008, 1:14am UTC](https://www.ruby-forum.com/t/attachment-fu-and-acts-as-list-a-land-war-in-asia/149541 "2008-09-26T01:14:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [September 26, 2008, 1:14am UTC](https://www.ruby-forum.com/t/attachment-fu-and-acts-as-list-a-land-war-in-asia/149541/1 "2008-09-26T01:14:34Z")

</div>

Hello-

I’m trying to get a list of attachment\_fu objects (type of  
ProductImage) to play well with acts\_as\_list. Essentially, a single  
Product can have multiple ProductImages arranged in a sorted list  
(which feeds into a slideshow, ultimately).

If it wasn’t clear from the title, this isn’t going well. The problem  
is in how attachment\_fu adds multiple records to the database (one for  
the main image and one for each thumbnail). I’ve been trying various  
scoping options to get this to work, but to no avail. Here is my  
latest try, which I had high expectations for:

acts\_as\_list :scope =\> ‘product\_id = #{product\_id} and parent\_id is  
null’

The error I get back is this:

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 ‘and parent\_id is null) ORDER BY position DESC LIMIT 1’ at  
line 1: SELECT \* FROM `product_images` WHERE (product\_id = and  
parent\_id is null) ORDER BY position DESC LIMIT 1

So, somewhere along the road I’m trying to retrieve things from the  
database, but passing in a null product\_id.

Does anyone know how to bring attachment\_fu and acts\_as\_list together  
as friends?

Thanks in advance.

-Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [September 26, 2008, 1:44am UTC](https://www.ruby-forum.com/t/attachment-fu-and-acts-as-list-a-land-war-in-asia/149541/2 "2008-09-26T01:44:59Z")

</div>

Got it!

This was a tricky one. I’ll post the solution here since I’ve seen  
some other posts where people got unsatisfactory results. I’m pleased  
with how this came out. Here is my ProductImage class, in its  
entirety.

class ProductImage \< ActiveRecord::Base

belongs\_to :product

acts\_as\_list

has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:max\_size =\> 500.kilobytes,  
:resize\_to =\> ‘480!x360!’,  
:thumbnails =\> {  
:medium =\> ‘247!x185!’,  
:small =\> ‘80!x60!’,  
:micro =\> ‘44!x33!’  
}

validates\_as\_attachment

def scope\_condition  
product\_condition = product\_id.nil? ? “product\_id is null” :  
“product\_id = #{product\_id}”  
product\_condition + " and parent\_id is null"  
end  
end

The magic is in the scope\_condition method that sensibly handles when  
we don’t have a product\_id (i.e., on the thumbnails that are getting  
ripped out).

-Eric

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [September 26, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/attachment-fu-and-acts-as-list-a-land-war-in-asia/149541/3 "2008-09-26T18:03:44Z")

</div>

This is nice but I’ve done a little different…I created one class  
for the image and another for the thumbnails. For this solution you  
need to add :thumbnail\_class =\> Thumbnail in the thumbnails hash and  
define the scope\_condition in the easy way (and a has\_many association  
too).
