Rails 3 / MySQL
I’ve never used autoinc (always provided my own random IDs), but now I
have to adapt to a legacy database using autoinc IDs.
Assuming the use of an autoinc ID field, I’ve read that after an
ActiveRecord model is saved for the first time, the ID value is known to
the model right away.
If not true, how could I or Rails possibly know how to create related
records w/o knowing the foreign key value?
If it is true, I am finding that to not be the case using a real simple
code sample like this.
new_user = PrivilegedUser.new
new_user.first_name = params[:first_name]
new_user.last_name = params[:last_name]
new_user.login_email = params[:login_email]
new_user.pswd1 = params[:pswd1]
new_user.pswd2 = params[:pswd2]
p new_user # ID is nil as expected
action_error = new_user.save
p new_user # ID is shown as 0, but MySQL says it is not 0
The model is saved, so it is not failing validations. A record is added
to MySQL and the ID column is int(11) AUTOINC and it does contain the
proper next value, not zero.
Something fishy going on, and I’m not sure where to start.
Any ideas what to investigate?