boblin
boblin

Reputation: 3541

routing in rails3 with custom primary id

I have model with custom primary key:

document.rb

class Document < ActiveRecord::Base
  set_primary_key "token"
end

routes.rb:

MyApp::Application.routes.draw do
  resources :documents, :only => [:index, :show, :create]
end

When i create new document, i get error:

No route matches {:controller=>"documents", :id=>#<Document id: "b430cfe73aaa5235fbfe", token: "b430cfe73aaa...

When i switch to use :id as a primary key, everything is OK. But I need to use token.

I use: rails 3.0.0 and ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-linux], MBARI 0x8770, Ruby Enterprise Edition 2010.02

Thanks for help.

Upvotes: 3

Views: 641

Answers (1)

Yannis
Yannis

Reputation: 5426

try to add to document.rb

def to_param
 token
end

Upvotes: 3

Related Questions