attachment_fu and acts_as_list - a land war in Asia

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

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

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).