<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sean&#039;s Blog &#187; validates_uniqueness_of</title>
	<atom:link href="http://seanbehan.com/tag/validates_uniqueness_of/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Validate Uniqueness on Join Tables in Rails</title>
		<link>http://seanbehan.com/ruby-on-rails/validate-uniqueness-on-join-tables-in-rails/</link>
		<comments>http://seanbehan.com/ruby-on-rails/validate-uniqueness-on-join-tables-in-rails/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 17:45:34 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[has_many]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[relationships]]></category>
		<category><![CDATA[through]]></category>
		<category><![CDATA[validates_uniqueness_of]]></category>

		<guid isPermaLink="false">http://bseanvt.wordpress.com/?p=171</guid>
		<description><![CDATA[How to validate uniqueness in a :has_many :through relationship with Ruby on Rails. You&#8217;ll need three models in this example List, Subscriber and ListSubscriber. A list has many subscribers and a subscriber has many lists. Simple enough. Running the following code would create a new record in our database table &#8220;list_subscribers&#8221;. It will also create [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fvalidate-uniqueness-on-join-tables-in-rails%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Validate Uniqueness on Join Tables in Rails" data-url="http://seanbehan.com/ruby-on-rails/validate-uniqueness-on-join-tables-in-rails/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>How to validate uniqueness in a :has_many :through relationship with Ruby on Rails. You&#8217;ll need three models in this example List, Subscriber and ListSubscriber. A list has many subscribers and a subscriber has many lists. Simple enough. Running the following code would create a new record in our database table &#8220;list_subscribers&#8221;. It will also create two records that are exactly the same.</p>
<pre class="wp-code-highlight prettyprint">
@subscriber.lists &lt;&lt; @list
@list.subscribers &lt;&lt; @subscriber
</pre>
<p>We need to correct this so that a subscriber can&#8217;t be put on a list twice. If we&#8217;re emailing our list, this subscriber would get two copies and we need to avoid this for obvious reasons. We need to add a validates_uniqueness_of condition in the model that represents the join table in the many to many relationship, ListSubscriber. ListSubscriber will take advantage of the :scope declaration and assign the :list_id as the qualifier, and throw an exception if we attempt to add a subscriber twice. Below is all the code you need to validate that there are no duplicates. Keep in mind that this is still application logic and therefore won&#8217;t add constraints to your database. It&#8217;s possible you&#8217;ll have duplicates in race conditions etc. This is a problem in general with validating uniquess in your application logic. Program it in your database to be certain!</p>
<pre class="wp-code-highlight prettyprint">
class List &lt; ActiveRecord::Base
  has_many :subscribers, :through =&gt; :list_subscribers
  has_many :list_subscribers
end

class Subscriber &lt; ActiveRecord::Base
  has_many :lists
  has_many :lists, :through =&amp;gt; :list_subscribers
end

class ListSubscriber&lt; ActiveRecord::Base
  belongs_to :list
  belongs_to :subscriber

  validates_uniqueness_of :subscriber_id, :scope =&gt; :list_id
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/validate-uniqueness-on-join-tables-in-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

