has_many_polymorphs and Rails 3

I’m gradually porting a number of my older Rails apps over to Rails 3. The main motivation is a chance to really put the new version through its paces, get a better sense of how it’s working, where plugins are at, etc; but it’s also rather nice to get some of the performance improvements and cleaner code along the way.

Catapult relies on Evan Weaver’s has_many_polymorphs plugin quite extensively so it was important to be have a Rails 3 compatible version. I couldn’t find any evidence that anyone else was working on it, so I’ve forked the github project and made a few alterations. I’ve set it up to work as a gem (so I can pull in the latest version using bundler) and adjusted to fit the new rails initialization process. It’s rather hacky, but it’s working for me so far.

Evan informs me that he has no plans to work on compatibility, so I’m going to see what time I can find to tidy it up a bit more, make sure the tests are passing, etc. If anyone else is so inclined, I’d love some help with that. You can find my fork on github.

Tags: , , ,

16 comments

  1. Sorry to hear he doesn’t intend to maintain it as I use it in just about everything as well. Good luck with your fork, I’ll be keeping an eye on it. One small niggle, however: his surname is Weaver…

  2. Ah – thanks. I’ve updated the post with the correct surname.

  3. I too use the Evan’s has_many_polymorphs gem with 2.3.8. It is indeed too bad that the original author is no longer interested in continued development. However myself and many others will likely be trying to get it working with Rails 3. I am going to give Ninja_Tune_XX’s fork a try and see how it fares.

    • Actually I see now that the forked version is from James Stewart. I added the line “gem ‘has_many_polymorphs’, :git => ‘http://github.com/jystewart/has_many_polymorphs.git’ ” to my gemfile and did bundle install and all went well. I have not done testing yet but it looks good so far.

  4. It is also working for me!

  5. You don’t have the issue tracker enabled on Github, so I’ll post it here.

    This plugin works for me, but the initializer fails (although there are no changes reagarding the models compared to the 2.3.10 version), it says “** has_many_polymorphs: User could not be preloaded: #”

    I also noticed in other classes that the class Foobar wasn’t available, and upon each restart of the web server, the *first* request to a URL which uses this class always fails. How can I avoid this, is there maybe a way to manually preload specific classes which other classes depend on?

    Thanks!

    • Yeah, there appear to be a few ongoing issues. I’m actually no longer using the plugin on the main project I needed it for so haven’t had time to maintain the fork. I’m very happy to take pull requests, or to point to another repository as “definitive”. I may get a chance to do more work on the plugin, but probably not till the summer.

      • Hi James,

        If you’ve stopped using Has Many Polymorphs on your main project, what plugin or methods did you use instead? There’s not a lot of options out there at the moment.

        Thanks,
        Chris

        • Working on a redesign of that project recently I realised that the pages I had needed has_many_polymorphs for were redundant, so it was probably best to lose the overhead of the gem. A few people have expressed interest in working on it a bit more so it may be worth searching around github to see if there any more active forks?

          (I wish I had a bit more time to at least tidy up the work I had done, but I’ve been out at SxSW the past week and as a result am not going to have much free time at all for the next few weeks)

  6. I’d like to start digging into this … maybe I can come up with a quick fix. What would you suggest where I start looking, how exactly does your plugin plug into* Rails’s init process?

    * pun intended

  7. There’s a railtie which adds the plugin to the initialization process. That may or may not be the best way to go as it might be better to hook into ActiveRecord directly.

    • Thanks – I actually tried to remove has_many_polymorphs also because of the performance in devel mode, but I’m a little confused: How would you specify the following

      has_many_polymorphs :user_actionables, :through => :user_actions, :from => [:search_actions, :contact_actions, :login_actions]

      without has_many_polymorphs? Basically, it’s a has_many association to a polymorphic model. Is this possible at all without the plugin?

  8. I used your gem for my rails3 upgrade. I have Sheets with Activities.
    class Activity [:sheets]
    end

    class Sheet < ActiveRecord::Base
    end

    I get an error if I do
    Sheet.last.activities
    NoMethodError: undefined method `activities'

    but if I do

    Activities.last.sheets.activities

    I get a list of Activities. So there must be an error of loading the the association the frist time from Sheet. Can I include it permanently in the class definition of Sheet?

    • @Kai, I’m not familiar with that syntax for declaring relationships? Is that really the entirety of your model definitions?

      • oh sorry, I did not check the posting after posting it. It should have been:

        class Activity < ActiveRecord::Base
        has_many_polymorphs :sheets
        end

        class Sheet < ActiveRecord::Base
        end

        I helped my self with a method loading Activity model:
        class Sheet < ActiveRecord::Base
        after_initialize :load_activity_model
        def load_activity_model
        Activity
        end
        end