Rails extension for mysql insert or update

| No Comments | No TrackBacks
Mysql has a special 'INSERT ... ON DUPLICATE KEY UPDATE' statement which can be used for some models like e.g. ratings. Instead of something similar to this:

You can just do this:

when the row already exists in the database (detected by a unique-constraint violation) the values of that row are updated and saved, otherwise the new row is created.

My current solution is this:

You use it like this:
Warning: You need mysql 5.1 so that LAST_INSERT_ID(id) works. (Mysql bug)

No TrackBacks

TrackBack URL: http://ragmaanir.mypresident.de/mt/mt-tb.cgi/10

Leave a comment