0

In the Rails docs, it says this:

<%= url_for(@workshop) %>
  # calls @workshop.to_s
  # => /workshops/5

Is this because the string version of every model in Rails corresponds to a URL for a particular instance of that model?

1 Answer 1

1

Well, the to_s method of Object prints out the class name. A Rails "model" is usually something that extends ActiveRecord::Base, and looking at the source code for that class shows it does not override to_s. My brain is too small to figure out what the source code for url_for is doing, but I suspect it is not just calling to_s on a model but rather doing more work to generate that URL. In Rails, URLs come from config/routes.rb so url_for must be using that in some way...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.