aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorHongli Lai (Phusion) <hongli@phusion.nl>2008-09-20 13:37:15 +0200
committerHongli Lai (Phusion) <hongli@phusion.nl>2008-09-20 13:37:15 +0200
commitbfa36d2f0bd9f6a39fbe8917e96478f5c1d9c3ed (patch)
treedd7d34deb05ffca6fb39ee27817142df982ba0c2 /activerecord
parent7a22f7abf3ea1f89f1b9b3b2d6d216fa12fd0b8e (diff)
downloadrails-bfa36d2f0bd9f6a39fbe8917e96478f5c1d9c3ed.tar.gz
rails-bfa36d2f0bd9f6a39fbe8917e96478f5c1d9c3ed.tar.bz2
rails-bfa36d2f0bd9f6a39fbe8917e96478f5c1d9c3ed.zip
Improve documentation for ActiveRecord::Base#to_param.
Diffstat (limited to 'activerecord')
-rwxr-xr-xactiverecord/lib/active_record/base.rb23
1 files changed, 22 insertions, 1 deletions
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
+ # <tt>map.resources :users</tt> 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