Last active
December 15, 2015 18:08
-
-
Save clowder/5300995 to your computer and use it in GitHub Desktop.
Polymorphic routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Child < ActiveRecord::Base | |
belongs_to :parent | |
has_many :leaves, :as => :branch | |
def route_parts | |
[parent, self] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Leaf < ActiveRecord::Base | |
belongs_to :branch, :polymorphic => true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parent < ActiveRecord::Base | |
has_many :children | |
has_many :leaves, :as => :branch | |
def route_parts | |
[self] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FooBar::Application.routes.draw do | |
resources :parents do | |
resources :leaves | |
resources :children do | |
resources :leaves | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment