tag:blogger.com,1999:blog-11788780.post108086983535430218..comments2023-12-29T13:22:33.104-08:00Comments on JJinuxLand: Rails: Handling Duplicate Entries with Unique Indexesjjinuxhttp://www.blogger.com/profile/03270879497119114175noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-11788780.post-36532993157927391462014-05-30T20:06:09.392-07:002014-05-30T20:06:09.392-07:00mwittig, nice!mwittig, nice!jjinuxhttps://www.blogger.com/profile/03270879497119114175noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-14238168785594075002014-05-29T15:06:47.695-07:002014-05-29T15:06:47.695-07:00A superior approach is to use MySQL's on-dupli...A superior approach is to use MySQL's on-duplicate-key-update functionality. This gem provides an interface to it:<br />https://github.com/zdennis/activerecord-import<br /><br />That way you don't get an error if there is a duplicate key, and you can overwrite values of other columns. If the record you're trying to save is at all different from the version in the database, the version in the database will not be modified since the save fails.mwittighttps://www.blogger.com/profile/00585250592732012554noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-55094580347887298702013-07-04T15:51:40.576-07:002013-07-04T15:51:40.576-07:00Thanks for your comment, but are you sure? I think...Thanks for your comment, but are you sure? I think the exception will get re-raised if it doesn't match "Duplicate entry".jjinuxhttps://www.blogger.com/profile/03270879497119114175noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-84216191558362571022013-07-01T23:31:19.588-07:002013-07-01T23:31:19.588-07:00I realize this is very old, but I stumbled upon it...I realize this is very old, but I stumbled upon it via google and noticed a potential snag with the code, so I will point it out: if the attributes are not the same as the existing record, they do not get updated in the database, but the method in the model swallows the exception...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-11788780.post-3988249177088977172009-09-09T01:20:23.637-07:002009-09-09T01:20:23.637-07:00By the way, here's the full exception:
Active...By the way, here's the full exception:<br /><br />ActiveRecord::StatementInvalid in ... Mysql::Error: Duplicate entry '1409-6622' for key 2: INSERT INTO `wishlist_books` (...) VALUES(...)jjinuxhttps://www.blogger.com/profile/03270879497119114175noreply@blogger.com