Topic: Writing Primary Key (user)

I'm writing an app where the primary key is not automaticlly incremented.  It will be a value (checked) taken from the user and put into the table.  Right now i cannot do that without unsetting the primary key which will then invalidate my search options.  So you can see here is the controller code

class Truck < ActiveRecord::Base
set_primary_key :vehicleID
  def before_create = vehicleID
  def before_update = vehicleID
    has_many :oils
  has_many :tows
  has_many :appearances
  has_many :brakes
  has_many :bridges
  has_many :inspections
  has_many :rdifferentials
  has_many :repairs
  has_many :treds

commenting out setting the primay key breaks all of my find methods
i've tried doing the following in the controller to write the primary key
<form method="post" action="create">

<b>Vehicle Number:</b><br/>
<input id="truck_vehicleID" name="truck[vehicleID]" size="20" type="number" value=""/><br/>

<input id="truck_vin" name="truck[vin]" size="20" type="number" value=""/><br/>

<b>Current Mileage:</b><br/>
<input id="truck_curmileage" name="truck[curmileage]" size="20" type="number" value=""/><br/>

<input id="truck_plate" name="truck[plate]" size="20" type="text" value=""/><br/>

<b>Oil Change Interval:</b><br/>
<input id="truck_oilInt" name="truck[oilInt]" size="20" type="number" value=""/><br/>

<input type="submit" value="Create">

now it doesn't seem to be passing the vehicleID proplerly or something.  I've tryed the controller many different ways and almost have it i think.  here is it in its original that wasn't working but i think should definately worked
    def create
    @a_truck =['truck'])
        redirect_to :action => 'list'
          render_action 'new'

here is what i have now (after many changes and eventually working back to almost what i have above.
    def create
    @a_truck = = :truck.vehicleID, @params['truck'])
        redirect_to :action => 'list'
          render_action 'new'

with the oringal i got the error message that in the table vehicleID is null and can't be with the new one i simple get that

undefined method `vehicleID' for :truck:Symbol

Re: Writing Primary Key (user)

I would recommend sticking with the Rails convention of always having an "id" column in a table as the auto-incrementing primary key (except in a HABTM relation table). I would also recommend sticking with all lowercase letters with underscores instead of CamelCase - I know it's not fun at first, but using the Rails conventions makes things so much easier. Maybe rename the vehicleID column to vehicle_number or something.

If you want to make sure the vehicle_number column is unique, use the validates_uniqueness_of method in your truck class.

class Truck
  validates_uniqueness_of :vehicle_number

See the docs for details.

Last edited by ryanb (2006-06-14 12:40:23)

Railscasts - Free Ruby on Rails Screencasts

Re: Writing Primary Key (user)

I agree with Ryan. Unless you are trying to interface with a legacy app, it is not really worth your time to force-fit a semantic primary key onto the rails model.

Re: Writing Primary Key (user)

I'll second that, err third it I guess.  When I first learnt Rails I didn't fancy some of the things it did so I just did it my way.  Eventually I realised that I was losing out on too much "automagic" and decided to go with the flow.