Search code examples
ruby-on-railspermalinks

Rails to_param using non-id field?


Instead of using a row's id field to generate slugs (like this):

class Person < ActiveRecord::Base
  def to_param
    [id, name.parameterize].join("-")
  end
end

I want to be able to use a non-id field, like this:

class Person < ActiveRecord::Base
  def to_param
    [application_id, name.parameterize].join("-")
  end
end

It's still an integer, but instead of doing a find based on the id, it should do a find based on application_id.


Solution

  • This is fine, but you'll have to amend your find code to use the application_id.

    @people = People.find_by_application_id(params[:id])
    

    This should solve your issue.