Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 13th, 2006, 2:34 PM   #1
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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 :truckymbol

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.
SirBob1701 is offline   Reply With Quote
Old Jun 13th, 2006, 3:27 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Jun 13th, 2006, 3:40 PM   #3
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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 is offline   Reply With Quote
Old Jun 14th, 2006, 9:21 AM   #4
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
Does anybody know how to do this? there HAS to be a way to do it its just freakin retarted if there isn't.
SirBob1701 is offline   Reply With Quote
Old Jun 14th, 2006, 10:33 AM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Have you tried asking on a RoR mailing list or forum?
Arevos is offline   Reply With Quote
Old Jun 14th, 2006, 10:38 AM   #6
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
Ya i got nothing not a single reply.
SirBob1701 is offline   Reply With Quote
Old Jun 14th, 2006, 11:16 AM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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?
Arevos is offline   Reply With Quote
Old Jun 14th, 2006, 11:24 AM   #8
SirBob1701
Newbie
 
Join Date: May 2006
Posts: 20
Rep Power: 0 SirBob1701 is on a distinguished road
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.
SirBob1701 is offline   Reply With Quote
Old Jun 14th, 2006, 11:27 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
I'll take a look in around an hour or two when I get home.
Arevos is offline   Reply With Quote
Old Jun 14th, 2006, 2:32 PM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:28 AM.

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