View Single Post
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: 5 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