我刚接触 rails,刚看到 db 和 model 这一块,有个问题: 常见到的例子如下:
Event 是一个脚本生成的 model
event = Event.new
event.field1 = 'str1'
event.field2 = 'data1'
...
event.save
=begin
irb 到这里提示如下,是其执行的 sql 查询命令吧:
(0.1ms) begin transaction
SQL (9.3ms) INSERT INTO "events" ("capacity", "created_at", "description", "is_public", "name", "updated_at") VALUE
S (?, ?, ?, ?, ?, ?) [["capacity", 20], ["created_at", Tue, 17 Jul 2012 16:12:25 UTC +00:00], ["description", "i love
you"], ["is_public", nil], ["name", "meme"], ["updated_at", Tue, 17 Jul 2012 16:12:25 UTC +00:00]]
(73.3ms) commit transaction
=end
put event.id #这里!event.id 值为 1 了
event.id 之前还是 nil,现在已经有值了,神马情况?上面的 sql 语句应该是不返回值的吧,那么这个自增的主键值是何时存入 id 里的?什么机制?这种机制能否避免在多线程环境下的竞争险象?