# How to create application with single table with primary key

**URL:** <https://www.ruby-forum.com/t/how-to-create-application-with-single-table-with-primary-key/86038>\
**Category:** Rails\
**Created:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/how-to-create-application-with-single-table-with-primary-key/86038 "2006-01-01T21:31:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Radek\_Hnilica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@Radek\_Hnilica](https://www.ruby-forum.com/u/Radek_Hnilica)\
**Post date:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/how-to-create-application-with-single-table-with-primary-key/86038/1 "2006-01-01T21:31:39Z")

</div>

I’m trying without any succes to create application in which I have  
one keyed table. The table structure is:

CREATE TABLE employees (  
pin INTEGER PRIMARY KEY,  
first\_name VARCHAR(30) NOT NULL,  
last\_name VARCHAR(30) NOT NULL  
);

Whenever I enter new record, I need the the filed pin is also filled  
by user. Whenever a user edit the table, there should be option to  
edit or not edit the pin field.

After creating a database in SQL server. Editing the model:  
class Employee \< ActiveRecord::Base  
has\_many :shots  
set\_primary\_key ‘pin’  
end

and creating the scaffold  
script/generate scaffold Employee

It simply does not work. When I try to access the  
[http://localhost:3000/employees/new](http://localhost:3000/employees/new) page, I got error

undefined method `pin\_before\_type\_cast’ for #Employee:0x4092d5d0

Extracted source (around line #5):

2:  
3:   
4:

Pin  
  
5: \<%= text\_field ‘employee’, ‘pin’ %\>

  
6:  
7: 

First name  
  
8: \<%= text\_field ‘employee’, ‘first\_name’ %\>

Can anyone point me to simple example how to create controller and  
view for editing simple keyed table?

# – Radek Hnilica [http://www.hnilica.cz](http://www.hnilica.cz)

No matter how far down the wrong road you’ve gone, turn back.  
Turkish proverb  
… so turn back … Now!

---

<div class="post-metadata">

**Author:** ![Radek\_Hnilica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@Radek\_Hnilica](https://www.ruby-forum.com/u/Radek_Hnilica)\
**Post date:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/how-to-create-application-with-single-table-with-primary-key/86038/2 "2006-01-01T21:31:55Z")

</div>

Not entirely sure I’m clear on what your trying to do, but that could  
just be that I had to festive of a weekend. That said, I think you’re  
going about this the wrong way.

In Rails, you typically have a primary key field called ID, that is a  
simple autoincrement nuimber field. If you name it ID, Rails will  
manage the keys for you, otherwise you need to tell Rails what to do.  
Also, your PIN shouldn’t be your Primary key field, especially if it’s  
used for any form of security, or if it will ever be changed.

Try adding a field ID and setting it to be the primary key, and have  
your PIN simply be an integer field. That should simplify the whole  
process for you.

---

<div class="post-metadata">

**Author:** ![Radek\_Hnilica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@Radek\_Hnilica](https://www.ruby-forum.com/u/Radek_Hnilica)\
**Post date:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/how-to-create-application-with-single-table-with-primary-key/86038/3 "2006-01-01T21:31:56Z")

</div>

On 12/26/05, Mike P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Try adding a field ID and setting it to be the primary key, and have  
> your PIN simply be an integer field. That should simplify the whole  
> process for you.

+1, and you can always add a unique constraint to the pin column in  
the database and/or a ‘validates\_uniqueness\_of :pin’ in the model  
class.

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer

* * *

Alice came to a fork in the road. “Which road do I take?” she asked.  
“Where do you want to go?” responded the Cheshire cat.  
“I don’t know,” Alice answered.  
“Then,” said the cat, “it doesn’t matter.”

- Lewis Carrol, Alice in Wonderland

---

<div class="post-metadata">

**Author:** ![Radek\_Hnilica](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@Radek\_Hnilica](https://www.ruby-forum.com/u/Radek_Hnilica)\
**Post date:** [January 1, 2006, 9:31pm UTC](https://www.ruby-forum.com/t/how-to-create-application-with-single-table-with-primary-key/86038/4 "2006-01-01T21:31:56Z")

</div>

On 12/26/05, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 12/26/05, Mike P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Try adding a field ID and setting it to be the primary key, and have  
> > your PIN simply be an integer field. That should simplify the whole  
> > process for you.
> 
> +1, and you can always add a unique constraint to the pin column in  
> the database and/or a ‘validates\_uniqueness\_of :pin’ in the model  
> class.

When you use set\_primary\_key to change the primary key column, you  
still get at the primary key value using the attribute “id” within  
ruby. So if you really need to have ‘pin’ be your primary key, then  
just add this to what you have:

alias\_method :pin, :id  
alias\_method :pin=, :id=

But only do this if you’re adapting a legacy table and don’t have  
control over it. If you control the table, then what everyone else  
here has said applies.

K
