How to create application with single table with primary key

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 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

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

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.

On 12/26/05, Mike P. [email protected] 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.


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

On 12/26/05, John W. [email protected] wrote:

On 12/26/05, Mike P. [email protected] 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