Forum: Ruby on Rails How to create application with single table with primary key

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eb3d791b9544a96a61c1566ad16914a1?d=identicon&s=25 Radek Hnilica (Guest)
on 2006-01-01 21:31
(Received via mailing list)
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: <!--[form:employee]-->
4: <p><label for="employee_pin">Pin</label><br/>
5: <%= text_field 'employee', 'pin'  %></p>
6:
7: <p><label for="employee_first_name">First name</label><br/>
8: <%= text_field 'employee', 'first_name'  %></p>

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


--
Radek Hnilica <Radek at Hnilica dot CZ>  http://www.hnilica.cz
==============================================================
No matter how far down the wrong road you've gone, turn back.
						Turkish proverb
... so turn back ... Now!
5179b3e6b326b0b919d031461a86b785?d=identicon&s=25 Mike Payson (Guest)
on 2006-01-01 21:31
(Received via mailing list)
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.
10d9ed7ab11115b081bb36f56a7a13bc?d=identicon&s=25 John Wilger (Guest)
on 2006-01-01 21:31
(Received via mailing list)
On 12/26/05, Mike Payson <mike@dawgdayz.com> 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 Wilger
http://johnwilger.com

-----------
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
6df19798ebd103cf103b6a48b15d9db7?d=identicon&s=25 Kenneth Miller (Guest)
on 2006-01-01 21:31
(Received via mailing list)
On 12/26/05, John Wilger <johnwilger@gmail.com> wrote:
> On 12/26/05, Mike Payson <mike@dawgdayz.com> 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
This topic is locked and can not be replied to.