<?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>[kumo.it] &#187; habtm</title>
	<atom:link href="http://www.kumo.it/articles/tag/habtm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kumo.it</link>
	<description></description>
	<lastBuildDate>Wed, 24 Aug 2011 21:38:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>rubyonrails &#8211; beware of similarly named relationships</title>
		<link>http://www.kumo.it/articles/2006/04/29/habtm-clobbering/</link>
		<comments>http://www.kumo.it/articles/2006/04/29/habtm-clobbering/#comments</comments>
		<pubDate>Sat, 29 Apr 2006 10:26:00 +0000</pubDate>
		<dc:creator>kumo</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[habtm]]></category>
		<category><![CDATA[notes]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I recently developed a &#8220;rubyonrails&#8221;:http://www.rubyonrails.org application in which a user had a province (as part of their address) and also a collection of selected provinces. class Users belongs_to :province has_and_belongs_to_many :selected_provinces, :class_name => "Province" end I could very easily find out which provinces a user had selected and also which users had selected a specific [...]]]></description>
			<content:encoded><![CDATA[<p>I recently developed a &#8220;rubyonrails&#8221;:http://www.rubyonrails.org application in which a user had a province (as part of their address) and also a collection of selected provinces.</p>
<pre lang="rails">
class Users
  belongs_to :province
  has_and_belongs_to_many :selected_provinces, :class_name => "Province"
end
</pre>
<p>I could very easily find out which provinces a user had selected and also which users had selected a specific province. However I didn&#8217;t realise that if I accessed a user via the province then the <code>province_id</code> which previously pointed the user&#8217;s address province was replaced by the id of the selected province.</p>
<pre lang="rails">
province = Province.find_first
province.users.each do |user|
  # do something
  # however user.province_id now points to province
end
</pre>
<p>I assume the <code>province_id</code> is added by the habtm join so that we can understand which selected_province object this user object comes from however in my case it meant that I lost the original <code>province_id</code> and so if I saved that user then I would end up changing the address province.</p>
<p>This is probably not a bug as such but something that should be watched out for as it took me a while to discover this problem with my application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kumo.it/articles/2006/04/29/habtm-clobbering/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 8/15 queries in 0.027 seconds using disk: basic

Served from: www.kumo.it @ 2012-02-04 21:10:37 -->
