Skip to content

Instantly share code, notes, and snippets.

@clowder
Last active December 15, 2015 18:08
Show Gist options
  • Save clowder/5300995 to your computer and use it in GitHub Desktop.
Save clowder/5300995 to your computer and use it in GitHub Desktop.
Polymorphic routes
class Child < ActiveRecord::Base
belongs_to :parent
has_many :leaves, :as => :branch
def route_parts
[parent, self]
end
end
class Leaf < ActiveRecord::Base
belongs_to :branch, :polymorphic => true
end
class Parent < ActiveRecord::Base
has_many :children
has_many :leaves, :as => :branch
def route_parts
[self]
end
end
FooBar::Application.routes.draw do
resources :parents do
resources :leaves
resources :children do
resources :leaves
end
end
end
= link_to 'New Leaf', url_for([:new] + record.route_parts + [:leaf])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment