Rails: id field is nil when calling Model.new
No, that's the correct behavior. When you create an object via new
(as in your example), Rails doesn't persist it to the database (just in memory).
If you do Message.create
, or Message.save
like theIV said, then the id
will be populated.
Like fig said,
n = Movie.new
n.save
=> true
means that it is saved and will be given an ID. Alternatively,
n = Movie.create!
automatically saves and stores it in the database and gives it an ID with one line of code.