# Mapping table column primary key in the model file

**URL:** <https://www.ruby-forum.com/t/mapping-table-column-primary-key-in-the-model-file/82755>\
**Category:** Ruby\
**Created:** [March 16, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/mapping-table-column-primary-key-in-the-model-file/82755 "2006-03-16T21:14:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 16, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/mapping-table-column-primary-key-in-the-model-file/82755/1 "2006-03-16T21:14:02Z")

</div>

I’m very new to RoR, and am trying to test out the scaffolding bit. I  
believe there’s an issue with my model file and the way I’m using  
ActiveRecord, as I’m unable to show specific details of a row. The  
table itself displays correctly when I do a ‘list’ on the webpage, but  
a ‘show’ for a specific row doesn’t work. My primary key (id) is not  
called ‘id’, its called ‘actor\_id’. So, when I try to do a show, or  
edit, I get the error :

ActiveRecord::RecordNotFound in Actor#show  
Couldn’t find Actor without an ID

How can I map my table column’s primary key id (called ‘actor\_id’) to  
‘id’ in the model file? I’ve tried using ‘id=actor\_id’, primary\_key,  
etc. in my model file (actor.rb) but nothing seems to work seemlessly,  
unless I modify the stubs that’re created when I run the ruby  
script\generate scaffold.

Here’s what I did to set things up :

1. I have a database called ‘test’, inside of which I have one table  
‘actors’ (I don’t know, just felt very hollywood-ish today). The  
columns in the table are:  
Actor\_id (primary key)  
Actor\_fname  
Actor\_lname  
Actor\_address

2. modify database.yml to point to the right database, host, table etc.

3. ruby script\generate model actor

4. ruby script\generate controller actor

5. add ‘scaffold:actor’ to actor\_controller.rb

6. ruby script\server

7. [http://localhost:3000/actor](http://localhost:3000/actor)

8. error occurs when I click on the ‘show’ link for one of the columns  
: ActiveRecord::RecordNotFound in Actor#show Couldn’t find Actor  
without an ID

I appreciate any help you can offer. Thanks in advance,

BYT

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 16, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/mapping-table-column-primary-key-in-the-model-file/82755/2 "2006-03-16T21:35:47Z")

</div>

unknown wrote:

> Couldn’t find Actor without an ID
> 
> How can I map my table column’s primary key id (called ‘actor\_id’) to  
> ‘id’ in the model file? I’ve tried using ‘id=actor\_id’, primary\_key,  
> etc. in my model file (actor.rb) but nothing seems to work seemlessly,  
> unless I modify the stubs that’re created when I run the ruby  
> script\generate scaffold.

This should really be psted on the Rails mailing  
list[[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)]

Try:  
set\_primary\_key ‘Actor\_id’

joey\_\_  
[http://www.feedreed.com](http://www.feedreed.com)
