DEV Community

Discussion on: Polymorphic Model Resource Finder in Ruby On Rails

Collapse
 
swiknaba profile image
Lud • Edited
return unless klass

Is never reached, because constantize raises an error if no constant with the given name exists. If you want to be able to return nil if no constant exists, use safe_constantize ;-)

One cool thing about strings in Ruby: you can easily extract substrings via regexs like this:

model = name[/([^\/.]*)_id$/, 1]