Ruby 使用Activerecord时获取最后一个插入id
对于SQILTE3CAPI,我将使用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”方法根据保存是
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