Question

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'?)

Was it helpful?

Solution

It's been long since the question was asked but I had the same problem recently and the solution was to use polymorphic_url. You need to find the name of the route you need to create a link to, for example "fast_car_path" and make it out of your *_type and *_id from polymorphic table. For example, you have a list of comments and want to make the link to the cars that they belong to. So if *_type = FastCar we have

@comments.each do |comment|
  link_to polymorphic_url(comment.commentable_type.tableize.singularize, :id => comment.commentable_id) 

which will generate "fast_car_path" without downloading the cars from database.

I am a noob in rails and I dont know how good that advice is, but I hope it will be helpful for somebody.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top