Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用Activerecord时获取最后一个插入id_Ruby_Sqlite_Activerecord - Fatal编程技术网

Ruby 使用Activerecord时获取最后一个插入id

Ruby 使用Activerecord时获取最后一个插入id,ruby,sqlite,activerecord,Ruby,Sqlite,Activerecord,对于SQILTE3CAPI,我将使用sqlite3\u last\u insert\u rowid。插入新记录后使用ActiveRecord时如何获取此id?我使用以下方法插入新记录: Section.new |s| s.a = 1 s.b = 2 #I expected the return value of save to be the last_insert_id, but it is NOT s.save end “save”方法根据保存是

对于SQILTE3CAPI,我将使用
sqlite3\u last\u insert\u rowid
。插入新记录后使用ActiveRecord时如何获取此id?我使用以下方法插入新记录:

Section.new |s|
     s.a = 1
     s.b = 2
     #I expected the return value of save to be the last_insert_id, but it is NOT
     s.save   
end

“save”方法根据保存是否成功返回true或false 试试smth,比如:

new_section = Section.new do |s|
    s.a = 1
    s.b = 2
    s.save   
end

p new_section 

它应该打印您手动设置的所有字段以及任何自动填充的值,包括last_insert_id通常是记录的“id”

假设您使用ActiveRecord默认字段名来保存主键,那么s.id将保存您刚刚保存的记录的id

s.id