开发者

get last insert id when using Activerecord

For Sqilte3 C API, I would use sqlite3_last_insert_rowid. How to get this id when using ActiveRecord after insert a new record? I use follo开发者_如何学Gowing way to insert a new record :

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' method returns true or false based on if saving was successful or not try smth like:

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

p new_section 

it should print all fields you set manually plus any automatically filled values, including last_insert_id being usually the record 'id'


Assuming you've used the ActiveRecord default field name to hold the primary key, then

s.id
will hold the id of the record you've just saved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜