Topic: Foreign Key not setting
This is my first stab at Ruby and I seem to be repeatedly doing something very wrong. My foreign keys never get anything in them.
My current test application has 2 simple tables.
USE testguy2_development;
DROP TABLE IF EXISTS firsttables;
CREATE TABLE firsttables (
id INT NOT NULL AUTO_INCREMENT,
firstfield VARCHAR(10) NOT NULL UNIQUE,
PRIMARY KEY (id)
);
CREATE TABLE secondtables (
id INT NOT NULL AUTO_INCREMENT,
secondfield VARCHAR(10) NOT NULL UNIQUE,
firsttable_id INT NOT NULL
PRIMARY KEY (id)
);
I generate the system with scaffold and updated the models
class Firsttable < ActiveRecord::Base
has_many :secondtables
end
class Secondtable < ActiveRecord::Base
belongs_to :firsttable
end
I can create records into either table fine but when I create records in secondtables, the field 'firsttable_id' is always null. I can see from the logs that I am passing the id to the controller if I call the 'new' method of secondtable explicitly from the firsttable show view.
Isn't the foreign key supposed to fill in automatically because it is named firsttable_id? Am I missing something really stupid here?