Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails get the id of the inserted row (not the last inserted) [duplicate]

In my model I have code that looks like this:

Report = Report.new()
Report.Name = name
Report.save()

This process happens several times in a second in parallel. If I get the last inserted id it gives me the wrong results. I don't want the id of the last inserted row in the database, I want the id of the actual inserted row. How do I do this?

like image 676
Kush Avatar asked Nov 30 '25 19:11

Kush


1 Answers

report = Report.new
report.name = name
report.save
p report.id # here you get the id
like image 153
xdazz Avatar answered Dec 03 '25 11:12

xdazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!