method

to_param

Importance_3
v2.1.0 - Show latest stable - 4 notes - Class: ActiveRecord::Base
to_param() public

Enables Active Record objects to be used as URL parameters in Action Pack automatically.

Show source
Register or log in to add new notes.
April 28, 2009
4 thanks

Tip: Define from_param(...) as Opposite

Often when defining a to_param method, it’s handy to introduce an opposite method for decoding them. For example:

class User < ActiveRecord::Base
  def self.from_param(param)
    find_by_name!(param)
  end

  def to_param
    name
  end
end

While you can just as easily redefine the find() method, this may be confusing since the expectation is that find() works with numerical IDs, or whatever the key column is defined as.

April 24, 2009
2 thanks

have your to_param begin with the object's id

If you overwrite the to_param method in your model class such that it does not begin with its id, you can be in for a nasty surprise:

Example

class User
  def to_param
    self.login
  end
  ...
end

Let’s say you have a user called “bob”, than you might think this works:

>> bob = User.find(3)
=> #<User id: 3, login: "bob", ...>
>> User.find(bob.to_param)
ActiveRecord::RecordNotFound: Couldn't find User with ID=bob

But it’s not the reason being that Rails find method looks for a beginning number (d+) and uses that to look up the record (and ignores everything that comes after the last digit). So the solution is to have your to_param return something that begins with the object’s id, like so:

Example

class User
  def to_param
    "#{self.id}-#{self.login}"
  end
  ...
end

>> bob = User.find(3)
=> #<User id: 3, login: "bob", ...>

>> User.find(bob.to_param)

> # id: 3, login: “bob”, …>

>> bob.to_param

> “3-bob”

May 30, 2013
1 thank

Avoiding to_param method when using URL helper methods

I recently found myself in the situation where I needed to generate URLs which included the ID instead of the value returned from the model’s to_param method (since someone had overridden the to_param method). It turned out to be easier than I thought. You can simply pass an ID to the helper method and it will construct the URL correctly:

edit_admin_foobar_path(@foobar.id)
# /admin/foobars/123/edit
March 5, 2009
1 thank

Mistake

It’s not:

user = User.find_by_name('Phusion')
user_path(path)  

It’s:

user = User.find_by_name('Phusion')
user_path(user)