![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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
endhere 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'
endhere 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'
endwith 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 ymboli 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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
Does anybody know how to do this? there HAS to be a way to do it its just freakin retarted if there isn't.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Have you tried asking on a RoR mailing list or forum?
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
Ya i got nothing not a single reply.
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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?
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2006
Posts: 20
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
I'll take a look in around an hour or two when I get home.
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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
endAs 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' |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|