views:

461

answers:

1

Here's my Schema

class Menu < ActiveRecord::Base
  belongs_to :menuable, :polymorphic => true
end

class Page < ActiveRecord::Base
  has_one :menu, :as => :menuable
end

class Links < ActiveRecord::Base
  has_one :menu, :as => :menuable
end

I want to link to a polymorphic class in the Menu view using link_to, e.g.

<%= link_to menu.name, menu.menuable %>

This works, but this retrieves the menuable object from the database, when all I wanted is to generate a link. You can imagine if my menu is large, this will really bog down my application.

When I decared the menuable field as polymorphic, Rails created menuable_type and menuable_id. What can I use to generate a link to the polymorphic page, short of writing a helper function with a giant switch statement (e.g. if I have a large number of menuable 'subclasses'?)

+1  A: 

You could do something like this:

def my_menu_url(menu)
  "/#{menu.menuable_type.tableize}/#{menu.menuable_id}"
end

if you use the rails convention for naming the controllers that correspondent to your models.

But don't do it. You work around the routing mechanism of rails and that's simply bad practice.

You should use the :include option in your finders to eager load your menuables:

Menu.all :include => :menuable

In the case this isn't enough you may use some sort of caching.

gregor
Does eager loading work for a polymorphic relationship? How does it know what table to join with, without first retrieving the menu table?
I guess at most you can reduce the loading to the number of polymorphic types that exist. Unfortunately, a quick search shows that this is not the existing behavior in ActiveRecord. :( Maybe it's time to go back to single table inheritance.
I think ActiveRecord first loads all the Menu records. In a second step it dos a query for every menuable_type in the result set of the first query. I think single table inheritance is no solution in this case, because the tow models differ to much.
gregor