views:

33

answers:

3

I've just generated my first scaffold in rails. Now, I don't want my table to have a field named "id". Instead I want it to be named "uid" which will be an arbitrary string. But I can't seem to get my head around how this is to be done. Especially with managing routes. I've managed to set :id=>false and define "uid" as the primary key but the controller fails because it still tries to lookup by id. How do I handle this?

Also, anything else I'm not aware of that might blow?

And, I know rails is all about convention and all and I must not try to go against "the rails way", but I really want this thing to work this way.

A: 

You should be able to find records by doing:

@record = Model.find('foo')

or

@record = Model.find_by_uid('foo')
John Topley
A: 

be sure that in your migration file you have:

create_table :table_name, :primary_key => :uid do |t|
...

then insert in your model:

self.primary_key = "uid"

and find your record as John Topley says:

@record = Model.find('foo')

This should work

baijiu
A: 

In your migration update your code like this:

create_table :table_name, :primary_key => :uid do |t|
...
end

And your model should include this line:

class ModelName < ActiveRecord::Base
  set_primary_key :uid
end

All will be working same as with normal id column.

Model.find will find by uid, not id and etc ...

retro