Created
September 13, 2013 04:56
-
-
Save cjavdev/6546895 to your computer and use it in GitHub Desktop.
first crack at a nested route for polymorphic association
This file contains 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
# my solution | |
get '/:notable_type/:noteable_id/notes', to: "notes#index" | |
@notes = Note.find_by_noteable_id_and_noteable_type(params[:notable_id], params[:notable_type]) | |
# notice how it doesn't matter if an invalid :notable_type is used because it will just return an empty collection of notes | |
# railscast solution, but doesn't catch case with nesting more than 2 deep... | |
def find_noteable | |
params.each do |name, value| | |
if name =~ /(.+)_id$/ | |
return $1.classify.constantize.find(value) | |
end | |
end | |
nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think dynamic finder methods like line 3 are being deprecated.