Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Ruby (http://www.programmingforums.org/forum63.html)
-   -   Primary Key Writing (http://www.programmingforums.org/showthread.php?t=10336)

SirBob1701 Jun 13th, 2006 3:34 PM

Primary Key Writing
 
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
    self.id = vehicleID
  end
 
  def before_update
    self.id = vehicleID
  end
        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
end



here is the view code pretty understandable
:

<form method="post" action="create">
<br>

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

<b>Vin:</b><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/>

<b>Tag:</b><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/>
</p>

<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.new(@params['truck'])
    if @a_truck.save
        redirect_to :action => 'list'
        else
          render_action 'new'
        end


here is what i have now (after many changes and eventually working back to almost what i have above.
:

    def create
    @a_truck = Truck.new(Truck.vehicleID = :truck.vehicleID, @params['truck'])
    if @a_truck.save
        redirect_to :action => 'list'
        else
          render_action 'new'
        end


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

i really need hlep with this if you need more information you can ask here or check a previous thread i had with my db design and original implementations.

Arevos Jun 13th, 2006 4:27 PM

I don't think you need the before_create and after_create functions.

I'd advise trying to assign the primary key manually:
:

def create
    @a_truck = Truck.new(@params['truck'])
    @a_truck.vehicleID = @params['truck']['vehicleID']
    if @a_truck.save
        redirect_to :action => 'list'
    else
        render_action 'new'
    end
end

See if that works.

SirBob1701 Jun 13th, 2006 4:40 PM

nope that doesn't work.
:

undefined method `vehicleID=' for #<Truck:0x36425c0>
RAILS_ROOT: ./script/../config/..

Application Trace | Framework Trace | Full Trace
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1789:in `method_missing'
#{RAILS_ROOT}/app/controllers/truck_controller.rb:64:in `create'


i tried something similar earlier but that didn't work either.

SirBob1701 Jun 14th, 2006 10:21 AM

Does anybody know how to do this? there HAS to be a way to do it its just freakin retarted if there isn't.

Arevos Jun 14th, 2006 11:33 AM

Have you tried asking on a RoR mailing list or forum?

SirBob1701 Jun 14th, 2006 11:38 AM

Ya i got nothing not a single reply.

Arevos Jun 14th, 2006 12:16 PM

I think the problem is that its difficult to diagnose a problem in isolation. The reason your program isn't working may possibly not be directly related to the primary key. Is it possible to provide the source and SQL schema for the system?

SirBob1701 Jun 14th, 2006 12:24 PM

Here are links for the shema download (mysql backup file) and the code ziped up
(note if your worried about viruses googlepages scans the files for them)
http://sirbob1701.googlepages.com/VehicleLog.zip
http://sirbob1701.googlepages.com/ba...0606141204.sql
I should also note in the schema that if you change the trucks tables primary key non of the finds work.

Arevos Jun 14th, 2006 12:27 PM

I'll take a look in around an hour or two when I get home.

Arevos Jun 14th, 2006 3:32 PM

I found a solution. It makes a strange sort of sense:
:

def create
    @a_truck = Truck.new(@params['truck'])
    @a_truck.id = @params['truck']['vehicleID']
    if @a_truck.save
        redirect_to :action => 'list'
    else
        render_action 'new'
    end
end

The "id" attribute doesn't appear to change if you alter the primary key, which I guess makes a bit of sense. As for setting it explicitly, I'd hazard a guess that's because the default activerecord constructor specifically avoids setting the primary key when given a hash. I guess that's to prevent accidental setting of the primary key when it's not intended. I can't say I approve...

As for your problem with linking the drivers table to the trucks table, you need a many-to-many relationship, which links two tables using an intermediate (ie. bridges). In Rails the function to do this is "has_and_belongs_to_many", which you should place in your truck.rb file:
:

has_and_belongs_to_many :drivers, :join_table => 'bridges', :foreign_key => 'driverssID', :association_foreign_key => 'vehicleID'
Edit: forgot the :drivers argument.


All times are GMT -5. The time now is 8:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC