Last Updated: February 25, 2016
·
529
· markets

Access polymorphic_path inside a model in Rails

It's not a good separation of concerns, but sometimes could be useful to access routes inside a model. Simply add to your model:

include Rails.application.routes.url_helpers

If you also need access to polymorphic routes:

include ActionDispatch::Routing::PolymorphicRoutes
include Rails.application.routes.url_helpers

def link_to_self
  polymorphic_path [self.parent, self]
end