Skip to content

Instantly share code, notes, and snippets.

@cjavdev
Created September 13, 2013 04:56
Show Gist options
  • Save cjavdev/6546895 to your computer and use it in GitHub Desktop.
Save cjavdev/6546895 to your computer and use it in GitHub Desktop.
first crack at a nested route for polymorphic association
# 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
@ofctlo
Copy link

ofctlo commented Oct 10, 2013

I think dynamic finder methods like line 3 are being deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment