acts_as_attachment error : undefined method `find_or_initialize_by_thumbnail_and_parent 1

Posted by Rajesh Shetty on August 21, 2007

You will possibly be seeing this error when you use acts_as_attachment for thumbnailing.

undefined method `find_or_initialize_by_thumbnail_and_parent’

Basically acts_as_attachment is trying to use dynamic finders ( Good article on that is here ) to find and create new object if it is not found. Developers use acts_a_attachment ( instead of attachument_fu; successor of acts_a_attachment works only Rails 1.2 onwards) because it works with older version of Rails (1.1.6) and when you execute thumbnailing you get above message and the reason the dynamic method find_or_initialize_by_thumbnail_and_parent that internally calls find_or_initialize which is available in Edge rails onwards and Rails 1.1.6 does not have that. So kinda defeats the purpose of using acts_a_attachment for Rails 1.1.6.

In any case don’t be disappointed and here is the fix ,so you can use everything as it is. Go to your vendors/plugins/acts_as_attachment/lib/technoweenie/acts_as_attachment/instance_methods.rb and look for method find_or_initialize_thumbnail , you need to patch this method to make it work with Rails 1.1.6 ( basically find and create new object seperately)

Replace following

   def find_or_initialize_thumbnail(file_name_suffix)        respond_to?(:parent_id) ?          thumbnail_class.find_or_initialize_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) :          thumbnail_class.find_or_initialize_by_thumbnail(file_name_suffix.to_s)

      end

with

        def find_or_initialize_thumbnail(file_name_suffix)        respond_to?(:parent_id) ?          thumbnail_class.find_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) ||thumbnail_class.new(:thumbnail=>file_name_suffix.to_s,:parent_id=>id) :thumbnail_class.new(:thumbnail=>file_name_suffix.to_s)      end

This should do the trick.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. […] 1.2 issue, this issue was there in acts_as_attachment as well and I have covered that in one of my previous post’s) . open attachment_fu.rb and replace […]

Comments