From bfa36d2f0bd9f6a39fbe8917e96478f5c1d9c3ed Mon Sep 17 00:00:00 2001 From: "Hongli Lai (Phusion)" Date: Sat, 20 Sep 2008 13:37:15 +0200 Subject: Improve documentation for ActiveRecord::Base#to_param. --- activerecord/lib/active_record/base.rb | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 556f9f115b..a08da7b9e2 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2274,7 +2274,28 @@ module ActiveRecord #:nodoc: end - # Enables Active Record objects to be used as URL parameters in Action Pack automatically. + # Returns a String, which ActionPack uses for constructing an URL to this + # object. The default implementation returns this record's id as a String, + # or nil if this record's unsaved. + # + # For example, suppose that you have a Users model, and that you have a + # map.resources :users route. Normally, +users_path+ will + # construct an URI with the user object's 'id' in it: + # + # user = User.find_by_name('Phusion') + # user_path(path) # => "/users/1" + # + # You can override +to_param+ in your model to make +users_path+ construct + # an URI using the user's name instead of the user's id: + # + # class User < ActiveRecord::Base + # def to_param # overridden + # name + # end + # end + # + # user = User.find_by_name('Phusion') + # user_path(path) # => "/users/Phusion" def to_param # We can't use alias_method here, because method 'id' optimizes itself on the fly. (id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes -- cgit v1.2.3