#33 new
Brett

STI and Attachment_fu: undefined method "attachment_options"

Reported by Brett | August 27th, 2008 @ 07:21 AM

I think that I may have stumbled upon a bug related to attachment_fu and single table inheritance (of course, I am relatively new at rails, so this could be an ignorant noob issue, not a bug.)

I have an STI model that has a Content superclass and subclasses of Article, SuccessStory, FeatureStory, and Video, like so: (you'll also notice that I am using thinking-sphinx, which may be involved in the issue as well)


class Content < ActiveRecord::Base

	define_index do
		indexes body
		indexes	title
		indexes headline
		indexes subhead
		set_property :delta => true
	end

end

class Article < Content
end

class SuccessStory < Content
end

class FeatureStory < Content
end

class Video < Content
	has_one :image
end

So as you can see, my Video class is connected to my Image model, as shown here:


class Image < ActiveRecord::Base
	belongs_to :page
	belongs_to :video
	belongs_to :paper

	has_attachment 	:storage => :file_system, :path_prefix => 'public/images/'

#	validates_as_attachment

end

Now, in my Video controller, when I show a Video object, I use the following code:


class VideosController < ApplicationController
	layout "loggedin"

	def show
		@video = Video.find(params[:id])
		@videos = Content.find(:all, :conditions => {:type => "Video"})
		@page = Page.find(6)

	end

And then in my view for videos/show, I use this code to create a link which will allow a visitor to watch the movie:


<%= link_to "+watch video (broadband)", @video.image.public_filename %>

The first time that I navigate to the page, everything works as would be expected... I get a link that says '+watch video (broadband)' that goes to the related file (in this case a video file). However, if I reload the page, I get the following error:


undefined method `attachment_options' for #<Image:0x260e1d4>

I have tried googling around the web for the better part of two days, and have not been able to track down the issue. The same thing occurs in another class that I have in my application, which also uses Attachment_fu and STI. I do have a class that does not use STI and also uses Attachment_fu, and it works properly every time, without fail.

Also of interest here is that, in the case of the Video object mentioned above, if I load up an object in the console and call video.image.public_filename, it also works every time. I can reload the same video object, or create new ones, and every time I call video.image.public_filename, it works fine. Its only in my rails application where it works the first time, and then fails every subsequent time.

TIA in advance for your assistance.

Brett

PS: A couple of other notes...

1) If I access other metadata through the video.image relationship (such as video.image.content_type, or video.image.filename), these work just fine, all the time, without fail, both in the console and in the rails application.

2) Another oddity that I have been experiencing in this particular application is its intermittent inability to see STI at all. I have used required "content" in my application controller to load the content model and its subclasses, but from time to time, my rails application fails to recognize them and I get "uninitialized constant" for the name of the controller that I am currently accessing and the STI model. If, however, I change required "content" to required "Content", quit mongrel and restart it, the uninitialized constant error goes away for a while. Then when it comes back, I just change required "Content" back to required "content", restart mongrel, and it works again. 3) I am running Ruby 1.8.7 with Rails 2.1 and the latest version of Attachment_fu downloaded today from Git. I installed my rails configuration with MySql5 via MacPorts.

Comments and changes to this ticket

  • sunday

    sunday August 27th, 2008 @ 08:31 PM

    • → Tag changed from “” to “attachment_options”

    i experience similar effects - suddenly. When i debug my rails application i set a breakpoint at (attachment_fu.rb)

    self.class.thumbnail.class

      # Returns the class used to create new thumbnails for this attachment.
      def thumbnail_class
      ->  self.class.thumbnail_class
      end
    

    stepping in

      def thumbnail_class
       -> attachment_options[:thumbnail_class] = attachment_options[:thumbnail_class].constantize unless attachment_options[:thumbnail_class].is_a?(Class)
        attachment_options[:thumbnail_class]
      end
    

    executing the first line here will jump to:

    def method_missing(method, *args, &block)
    ->  raise_nil_warning_for METHOD_CLASS_MAP[method], method, caller
    end
    

    in whiny_nil.rb

    the resulting error is:

    You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base.

    Any idea why this error suddenly happens? And mostly when reloading a page?

    I am looking since days for a solution...

  • Brett

    Brett August 28th, 2008 @ 10:56 PM

    Hmmm... interesting. I am not knowledgeable enough of a programmer to dig that deep into the plug in, but one additional observation that I could add is the fact that none of my items being uploaded through attachment_fu have thumbnails.

    I don't know if that would shed any additional light on the subject, but since the error you noted happens when dealing with attachment_options[:thumbnail_class], I thought I would mention it.

    Are you using thumbnails with your application?

    b

  • sunday

    sunday August 29th, 2008 @ 06:21 AM

    yes i'm using thumbnails. In the meantime i get "nil object..." errors with a pointer to the "public_filename" method as well. Sometimes the method is found, sometimes not.

    Don't know what went wrong - it drives me insane...

  • Brett

    Brett August 30th, 2008 @ 03:13 AM

    What other plug-ins are you using? The ones that I have are as follows:

    • acts_as_tree
    • thinking_sphinx
    • will-paginate

    Are you using any of these plug-ins as well?

    Also, I found a web page that describes a somewhat similar issue and I am wondering if this could help lead us to a solution to our problem:

    http://blog.noizeramp.com/2008/0...

    Thanks,

    Brett

  • Brett

    Brett August 30th, 2008 @ 03:32 AM

    Also, a couple of other comments:

    1) I am saving files to the file system (not to the database or S3) 2) I am running on Mac OS X Leopard 3) I'm also using TinyMCE (Thought I can't imagine that this would be an issue because it is all JavaScript)

    Brett

  • Brett

    Brett August 30th, 2008 @ 03:49 AM

    Ok, since I have been spending a fair amount of time looking at this, it would seem that what is happening is that for some reason, after the first time the data is loaded, attachment_options is no longer available to the backend as a method.

    In the attachment_fu.rb file, line 59 has a comment that states 'doing these shenanigans tso that #attachment_options is available to processors and backends'. The part specific to backends seems to be between lines 60 and 79.

    Now right before this, lines 55 and 56, there is some class method creation / access / linking going on... I wonder if this is some how playing into the mix.

    Brett

  • sunday

    sunday August 30th, 2008 @ 10:28 AM

    I am also saving files in the filesystem, running on mac osx 10.5 and using tiny mce...

    But i do not use the plugin's you mentioned.

    I also wondered about the 'doing these shenanigans' thingy and the behaviour you described. Loading a page which involves attachment_options does sometimes well for first time. A reload throws the no method error.

    strange

  • sunday

    sunday August 31st, 2008 @ 05:55 PM

    in the meantime i have restored some backups of my app to exclude any errors in here. These versions have been backuped in running condition.

    Guess what... every version and configuration fails with that error. I load the page one time everything works, second time crash.

    now i wonder - what happened with my configuration ? i did not updated rails, ruby or anything else... Why does this error appear suddenly without intervention ?

    Looks like the ruby world is'nt trustworthy as expected. I really consider of changing the plattform since this behaviour can kick your a** in a productive environment.

    Furthermore it seems, that the ruby and rails community is'nt really interested in such issues... I often read about that or similar issues but was not able to find a reaction or a solution anywhere.

    BTW is use Ruby 1.8.6 (2007-09-24 patchlevel 111) universal darwin 9.0

    and rails 2-1-0

  • Brett

    Brett August 31st, 2008 @ 06:25 PM

    • → Tag changed from “attachment_options” to “attachment_fu attachment_options bug crash "undefined method"”
    • → Assigned user changed from “” to “rick”

    In general, I have found the community to be quite supportive and on top of many issues. However, since Rails / Attachment_fu, and all the other plug-ins and gems are open source, your mileage may vary in regards to support.

    For me, what I will probably be doing is re-factoring my code so as to not use attachment_fu with models that use single table inheritance, since at this point, it is clearly not reliable. Quite a bit of additional work that I was hoping to avoid, but since it would take me at least 4x as much time to re-do the entire application that I am building in another language, I really have little choice.

    Anyway, I hope that someone, maybe Rick himself, will have some time in their busy schedules to have a look at this issue and see what may be causing it. Obviously, its a show-stopper bug for Attachment_fu, and makes the plug-in considerably less useful. I just wish that I was skilled enough a programmer to be able to dig into it and try and uncover the issue, but I really don't understand much of the code in the plug-in.

    BTW, do you know if we actually have to assign the ticket to someone? There is a 'Who's responsible?' pull-down menu that lists a number of people, and I wonder if we actually have to select someone to get their attention? Well, I'm going to give it a shot and see what happens... I don't know if that is the proper protocol or not.

    Brett

  • sunday

    sunday August 31st, 2008 @ 06:50 PM

    refactoring is unfortunately not an option to me, since i utilize a standard plugin (comunityengine) which uses attachment_fu this way.

    I do not want to start bashing in any direction here - it's just the typical open source problem i often experienced. Not comprehensible issues in situations where it fits absolutely not.

    Hope this issue get's the attention it needs to get solved.

    Dont know if we have to assign this ticket - i would'nt know to whom either...

    I wonder if we can't classify this issue in blocking, non blocking or something like that to seperate between important issues , important and urgent issues and cosmetics.

    Let's see and hope

  • Brett

    Brett September 1st, 2008 @ 04:07 PM

    Ok, so I just took one of my models that was part of my single table inheritance structure and moved it out so that it inherits from ActiveRecord::Base. Relaunched my application and now it works perfectly, every time. No undefined_method error.

    While this seems to just confirm the obvious, it appears that attachment_fu, in its current iteration, is simply not compatible with STI in version 2.1 of Rails.

    Hopefully this bug will get addressed in the near future, because it is a tremendously useful plug-in and I use it quite frequently.

    Brett

  • Brett

    Brett September 1st, 2008 @ 04:14 PM

    Ok, just tried a variation of the above.

    Using my newly created model file, I had it inherit from my previous STI class (so now my class structure looks like this: TechnicalReport < Publication < ActiveRecord::Base). Tried loading multiple pages of Technical Reports and this approach works just fine as well.

    Seems to be that the bug is related to having the STI classes defined in the same .rb file. If you put your models in separate files then STI seems to work just fine.

    Brett

  • sunday

    sunday September 1st, 2008 @ 05:55 PM

    followed your first approach (removing sti) yesterday.

    This works for me also, even if this is no durable solution. I just wanted to check if we both really experienced the same cause & effect. We do...

    Your second proposal sounds very promising to me, this could be a solution. I will figure that out tonite ^^

    Thanks a lot for sharing your thoughts and approaches!

    Martin

  • sunday

    sunday September 1st, 2008 @ 07:53 PM

    got more errors like this, this time without involving attachment_fu ...

    First time execution direct after restart works well, reload crashes...

    An example (happens with several pages)

    NoMethodError in Users#show

    Showing posts/_post.html.haml where line #5 raised:

    undefined method user' for #<Post:0x320e240>

    Extracted source (around line #5):

    2: - if post_counter == 1 3: 4: .category 5: = link_to post.user.login, user_path(post.user) 6:

    Trace of template inclusion: /users/show.html.haml

    RAILS_ROOT: /Users/xyz/NetBeansProjects/projectname

    /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/attribute_methods.rb:256:in method_missing' app/views/posts/_post.html.haml:5:inrun_plugin_47app47views47posts47_post46html46haml' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/base.rb:338:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/base.rb:338:inexecute' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/template_handlers/compilable.rb:29:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/template_handlers/compilable.rb:29:inrender' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/partial_template.rb:20:in render' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:26:inbenchmark' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/benchmark.rb:8:in realtime' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:26:inbenchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/partial_template.rb:19:in render' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/template.rb:22:inrender_template' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/partial_template.rb:28:in render_member' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/partials.rb:142:inrender_partial_collection_with_known_partial_path' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/partials.rb:141:in map' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/partials.rb:141:inrender_partial_collection_with_known_partial_path' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/partials.rb:135:in render_partial_collection' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/base.rb:271:inrender_without_haml' /Library/Ruby/Gems/1.8/gems/haml-2.0.1/lib/haml/helpers/action_view_mods.rb:5:in render' /Library/Ruby/Gems/1.8/gems/haml-2.0.1/lib/haml/helpers.rb:57:innon_haml' /Library/Ruby/Gems/1.8/gems/haml-2.0.1/lib/haml/helpers/action_view_mods.rb:5:in render' app/views/users/show.html.haml:89:inrun_plugin_47app47views47users47show46html46haml' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/base.rb:338:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/base.rb:338:inexecute' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/template_handlers/compilable.rb:29:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/template_handlers/compilable.rb:29:inrender' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/template.rb:35:in render' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/template.rb:22:inrender_template' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_view/base.rb:245:in render_file' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1108:inrender_for_file' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:841:in render_with_no_layout' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/layout.rb:251:inrender_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/benchmark.rb:8:inrealtime' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1157:indefault_render' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:1163:in perform_action_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:580:incall_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:601:in run_before_filters' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/callbacks.rb:173:insend' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/callbacks.rb:173:in evaluate_method' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:395:incall' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:598:in run_before_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:578:incall_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:573:in perform_action_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:68:inperform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/rescue.rb:201:inperform_action_without_caching' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:13:in perform_action' /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:33:incache' /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/query_cache.rb:8:in cache' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:12:inperform_action' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:inprocess_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:569:in process_without_session_management_support' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/session_management.rb:130:insass_old_process' /Library/Ruby/Gems/1.8/gems/haml-2.0.1/lib/sass/plugin/rails.rb:19:in process' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:389:inprocess' /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/webrick_server.rb:78:in service' /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/servers/webrick.rb:66 /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:inrequire' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in new_constants_in' /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:inrequire' /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/server.rb:39

    rails 2-1-0 forgets the method...

    Does anybody have any clue why and how to fix that?

    This all makes no sense to me - help!

  • sunday

    sunday September 1st, 2008 @ 07:55 PM

    OK - the formatting feature in lighthouse is experimental as well...

    trying again...

    # 2: - if post_counter == 1 3: 4: .category 5: = link_to post.user.login, user_path(post.user) 6: 7: = image_tag((post.first_attached_photo(post.id) || post.user.avatar_photo_url(:thumb)), :alt=>"", :width=>100, :height=>100) 8:

  • sunday

    sunday September 1st, 2008 @ 07:57 PM

    no way... forget it - enough frustration for today

  • sunday

    sunday September 1st, 2008 @ 10:03 PM

    after spending additional 4 hours in debugging i detected the same sti issue - which does not only apply to attachment_fu.

    method missing appears which every class which has been STI'd by me. As soon as i dropped my model and controllers in my app directory and the plugin falls back to use it's own everything works well. So it was my modification of the model to STI which enforces the whole application to forget methods randomly. Really bizarre.

    So for me everything works well - so far. Even if i dont understand what's really going in here. And to be honest - i dont know if i can trust this situation in my productive environment.

    I would really like to have some explanation from the genius guys here..

    best

    martin

  • Brett

    Brett September 1st, 2008 @ 10:25 PM

    One quick note on the STI... are you using a require statement in your application controller?

    In my application.rb file, I have the following at the top of the file:

    require 'content' require 'publication'

    In this case, 'content' and 'publication' are the name of the models that include my STI classes, and they both inherit from ActiveRecord::Base. So far, these have been working ok for me.

    Brett

    On Sep 1, 2008, at 2:04 PM, Lighthouse wrote:

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket