<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Working around the validates_uniqueness_of bug in Ruby on Rails</title>
	<atom:link href="http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/</link>
	<description>Michael Hartl&#039;s tech blog</description>
	<lastBuildDate>Wed, 21 Dec 2011 11:47:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: swapnil</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-466</link>
		<dc:creator><![CDATA[swapnil]]></dc:creator>
		<pubDate>Thu, 20 Jan 2011 08:46:28 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-466</guid>
		<description><![CDATA[I have a table with student_id and class_id. I want to check that student doesn&#039;t belong to more than 1 class i.e student_id and class_id combination will be only once in my table. how to ensure this using validate_uniqueness_of  ?]]></description>
		<content:encoded><![CDATA[<p>I have a table with student_id and class_id. I want to check that student doesn&#8217;t belong to more than 1 class i.e student_id and class_id combination will be only once in my table. how to ensure this using validate_uniqueness_of  ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-186</link>
		<dc:creator><![CDATA[Peter]]></dc:creator>
		<pubDate>Tue, 07 Apr 2009 12:31:31 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-186</guid>
		<description><![CDATA[I wrote a small AR::Base method to make it easier to use the method described above.  I don&#039;t have a blog, so I put it here: http://stackoverflow.com/questions/724431/how-can-i-determine-if-my-activerecord-object-violates-a-unique-database-key-inde]]></description>
		<content:encoded><![CDATA[<p>I wrote a small AR::Base method to make it easier to use the method described above.  I don&#8217;t have a blog, so I put it here: <a href="http://stackoverflow.com/questions/724431/how-can-i-determine-if-my-activerecord-object-violates-a-unique-database-key-inde" rel="nofollow">http://stackoverflow.com/questions/724431/how-can-i-determine-if-my-activerecord-object-violates-a-unique-database-key-inde</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-185</link>
		<dc:creator><![CDATA[Peter]]></dc:creator>
		<pubDate>Tue, 07 Apr 2009 03:28:09 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-185</guid>
		<description><![CDATA[Since this is a fundamental problem in Rails, would be nice if there were a standard way to catch this error.  Trying to parse the error message makes sense, but is not very satisfactory.

I don&#039;t have a slam dunk solution, but one useful suggestion is to catch AR::StatementInvalid, and then in your rescue block check for a duplicate record in the database.  If you find a duplicate record, chances are that&#039;s the reason for the exception and you can handle it accordingly, whereas if you don&#039;t find a duplicate, then the AR::StatementInvalid must be for some other reason and my inclination would be to reraise.]]></description>
		<content:encoded><![CDATA[<p>Since this is a fundamental problem in Rails, would be nice if there were a standard way to catch this error.  Trying to parse the error message makes sense, but is not very satisfactory.</p>
<p>I don&#8217;t have a slam dunk solution, but one useful suggestion is to catch AR::StatementInvalid, and then in your rescue block check for a duplicate record in the database.  If you find a duplicate record, chances are that&#8217;s the reason for the exception and you can handle it accordingly, whereas if you don&#8217;t find a duplicate, then the AR::StatementInvalid must be for some other reason and my inclination would be to reraise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vanicicsesode</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-168</link>
		<dc:creator><![CDATA[vanicicsesode]]></dc:creator>
		<pubDate>Sat, 20 Dec 2008 04:13:57 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-168</guid>
		<description><![CDATA[Hello! simply super resource]]></description>
		<content:encoded><![CDATA[<p>Hello! simply super resource</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mhartl</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-50</link>
		<dc:creator><![CDATA[mhartl]]></dc:creator>
		<pubDate>Wed, 23 Jul 2008 20:28:22 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-50</guid>
		<description><![CDATA[@Henrik: &#039;/&#039; is sensible in our app (Insoshi), but of course your case may be different.  Thanks for the JavaScript tip; we may end up incorporating that at some point.

@Robin Ward: Thanks for the note.  We&#039;re doing our best to be PostgreSQL-compatible (harder than it sounds), so we do want to avoid MySQL-specific error handling at this point.  But you&#039;re right, that comes at a cost, since the exception we catch is quite general.  Regarding the error-stashing: we&#039;ve found that most duplicates are inadvertent, so we prefer to ignore them silently, but your approach is certainly valid.]]></description>
		<content:encoded><![CDATA[<p>@Henrik: &#8216;/&#8217; is sensible in our app (Insoshi), but of course your case may be different.  Thanks for the JavaScript tip; we may end up incorporating that at some point.</p>
<p>@Robin Ward: Thanks for the note.  We&#8217;re doing our best to be PostgreSQL-compatible (harder than it sounds), so we do want to avoid MySQL-specific error handling at this point.  But you&#8217;re right, that comes at a cost, since the exception we catch is quite general.  Regarding the error-stashing: we&#8217;ve found that most duplicates are inadvertent, so we prefer to ignore them silently, but your approach is certainly valid.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robin Ward</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-49</link>
		<dc:creator><![CDATA[Robin Ward]]></dc:creator>
		<pubDate>Wed, 23 Jul 2008 14:40:47 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-49</guid>
		<description><![CDATA[I think any Rails sites that deal with a lot of concurrency will have to deal with this issue at some point.

Catching StatementInvalid alone will work if you violate an index, but it will also fire if somehow you have cocked up your SQL.

Unfortunately, it seems each database has its own version of the error that occurs when you violate an index. But if you know you are going to use MySQL, you can do something like this:

rescue ActiveRecord::StatementInvalid =&gt; error
  if error.to_s.include?(&quot;Mysql::Error: Duplicate entry&quot;)
     # raise your own exception

Note that a more graceful way than re-directing would be to stash something in the errors collection. That&#039;s how I do it anyway :)]]></description>
		<content:encoded><![CDATA[<p>I think any Rails sites that deal with a lot of concurrency will have to deal with this issue at some point.</p>
<p>Catching StatementInvalid alone will work if you violate an index, but it will also fire if somehow you have cocked up your SQL.</p>
<p>Unfortunately, it seems each database has its own version of the error that occurs when you violate an index. But if you know you are going to use MySQL, you can do something like this:</p>
<p>rescue ActiveRecord::StatementInvalid =&gt; error<br />
  if error.to_s.include?(&#8220;Mysql::Error: Duplicate entry&#8221;)<br />
     # raise your own exception</p>
<p>Note that a more graceful way than re-directing would be to stash something in the errors collection. That&#8217;s how I do it anyway :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrik N</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-48</link>
		<dc:creator><![CDATA[Henrik N]]></dc:creator>
		<pubDate>Wed, 23 Jul 2008 13:18:00 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-48</guid>
		<description><![CDATA[I don&#039;t agree that directing somewhere like &#039;/&#039; is sensible. That would mean users that click multiple times risk being sent out of the the registration flow (or whatever the form is for).

Instead, this is what I&#039;m doing:

1. database-level uniqueness constraint to ensure data integrity

2. &lt;a href=&quot;http://henrik.nyh.se/2008/07/jquery-double-submission&quot; rel=&quot;nofollow&quot;&gt;JavaScript to mitigate the issue&lt;/a&gt;

I should probably also catch &lt;code&gt;ActiveRecord::StatementInvalid&lt;/code&gt; (if the user doesn&#039;t have JS) and do something useful like redirect them to the next step of the registration process for the user with the details they just resubmitted.

By the way, I&#039;ve seen this issue from a user using IE7, but I can&#039;t reproduce with Firefox 3. Possibly Firefox queues up the HTTP requests, waiting for one (the first submit) to complete before starting the next. So instead of an database level error, I would get a Rails-level validation error about the e-mail already being in use. My JS mitigates that as well.]]></description>
		<content:encoded><![CDATA[<p>I don&#8217;t agree that directing somewhere like &#8216;/&#8217; is sensible. That would mean users that click multiple times risk being sent out of the the registration flow (or whatever the form is for).</p>
<p>Instead, this is what I&#8217;m doing:</p>
<p>1. database-level uniqueness constraint to ensure data integrity</p>
<p>2. <a href="http://henrik.nyh.se/2008/07/jquery-double-submission" rel="nofollow">JavaScript to mitigate the issue</a></p>
<p>I should probably also catch <code>ActiveRecord::StatementInvalid</code> (if the user doesn&#8217;t have JS) and do something useful like redirect them to the next step of the registration process for the user with the details they just resubmitted.</p>
<p>By the way, I&#8217;ve seen this issue from a user using IE7, but I can&#8217;t reproduce with Firefox 3. Possibly Firefox queues up the HTTP requests, waiting for one (the first submit) to complete before starting the next. So instead of an database level error, I would get a Rails-level validation error about the e-mail already being in use. My JS mitigates that as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simple double form submission prevention with jQuery &#8211; The Pug Automatic</title>
		<link>http://blog.mhartl.com/2008/06/26/working-around-the-validates_uniqueness_of-bug-in-ruby-on-rails/#comment-47</link>
		<dc:creator><![CDATA[Simple double form submission prevention with jQuery &#8211; The Pug Automatic]]></dc:creator>
		<pubDate>Wed, 23 Jul 2008 13:08:23 +0000</pubDate>
		<guid isPermaLink="false">http://insoshi.wordpress.com/?p=12#comment-47</guid>
		<description><![CDATA[[...] I recently ran into one such issue. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] I recently ran into one such issue. [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

