Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Polymorphic relationship and link_to

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

like image 752
tchen Avatar asked Mar 01 '23 10:03

tchen


1 Answers

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.

like image 199
NoDisplayName Avatar answered Mar 05 '23 13:03

NoDisplayName