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?

-- gw

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to