has_many_polymorphs: Tag could not be preloaded: #<ActiveRecord::Associations::PolymorphicError

Hello there,

I've also posted this problem at the hmp forum @
http://rubyforge.org/forum/forum.php?thread_id=22433&forum_id=16450
but since this group has some higher traffic, maybe the one or another
can help me here, too.

I have a little rails (2.0.2) app with very few models so far, one
being a user model which has_many text_snippets and also has_many
bookmarks. Resources wise, these two are nested under the users .. so
callable via /users/1/bookmarks/15 etc.

I ran the hmp tagging generator with the two models Bookmark and
Textsnippet which led to this entry in the tag.rb file:

  has_many_polymorphs :taggables,
    :from => [:bookmarks, :text_snippets],
    :through => :taggings,
    :dependent => :destroy,
    :skip_duplicates => false,
    :parent_extend => proc {
      # Defined on the taggable models, not on Tag itself. Return the
tagnames associated with this record as a string.
      def to_s
        self.map(&:name).sort.join(Tag::DELIMITER)
      end
    }

So far so good. However, when starting the server, this is being
logged:

** has_many_polymorphs: rails environment detected
** has_many_polymorphs: loaded ok
** has_many_polymorphs: autoload hook invoked
** has_many_polymorphs: preloading parent model Tag
** has_many_polymorphs: associating Tag.taggables
** has_many_polymorphs: Tag could not be preloaded:
#<ActiveRecord::Associations::PolymorphicError: Could not find a valid
class for :bookmarks. If it's namespaced, be sure to specify it
as :"module/bookmarks" instead.>
** has_many_polymorphs: preloading parent model Tagging
** has_many_polymorphs: autoload hook invoked
** has_many_polymorphs: preloading parent model Tag
** has_many_polymorphs: associating Tag.taggables
  SQL (0.028267) SET NAMES 'utf8'
  SQL (0.010828) SET SQL_AUTO_IS_NULL=0
  Bookmark Columns (0.103882) SHOW FIELDS FROM `bookmarks`
  Tagging Columns (0.018256) SHOW FIELDS FROM `taggings`
  TextSnippet Columns (0.019460) SHOW FIELDS FROM `text_snippets`
** has_many_polymorphs: preloading parent model Tagging

Now neither the text_snippets nor the bookmarks are taggable (double-
checked via console). However, this error is somewhat misleading
or ... well.. dunno.. the Bookmark class does in fact exist:

Loading development environment (Rails 2.0.2)

Bookmark.inspect

=> "Bookmark(id: integer, user_id: integer, url: string, title:
string, description: text, created_at: datetime, updated_at:
datetime)"

I am really kind of confused now where to start digging/looking...
tried all I could, double-checked whether bookmarks' files are all
correct.. which they are.. but still it ain't working.

Has anyone else come across this somewhere/somehow? Any ideas/
suggestions? :slight_smile:

-J

Ok - found it :-/

I have a custom validation in my Bookmark model. This validation is
declared in lib/custom_validations.rb ... which is require'ed in
environment.rb. Now hmp (the plugin) gets loaded before the
custom_validations.rb file is loaded.. and therefore the Bookmark
class looks b0rked at that very moment.

Once rails has in fact loaded the custom_validations.rb file,
everything works fine at the Bookmark-class level.. but hmp actually
isn't.

Hrrmm - what can I do now? I mean... I can't be the only one using
custom validations in the lib/ folder in combination with hmp... :-/

-j

In Rails 2.x you should be able to use initializers (/config/
initializers + environment) to control the load order. Likely a well-
placed 'require' will solve the problem.