<?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: Self-referential many-to-many relations in Ruby on Rails</title>
	<atom:link href="http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/</link>
	<description>The blog with the bug.</description>
	<lastBuildDate>Thu, 23 May 2013 14:32:44 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Guillaume</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-1265</link>
		<dc:creator><![CDATA[Guillaume]]></dc:creator>
		<pubDate>Thu, 19 Jan 2012 02:27:08 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-1265</guid>
		<description><![CDATA[Many many thanks!]]></description>
		<content:encoded><![CDATA[<p>Many many thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: christian</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-1089</link>
		<dc:creator><![CDATA[christian]]></dc:creator>
		<pubDate>Thu, 18 Feb 2010 09:57:31 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-1089</guid>
		<description><![CDATA[still very usefull. having mine half way done, i&#039;ve found yours and it helped a lot to get mine fully done. i&#039;ve solved(hopefully?) the bidirectional thing by using not rails but ruby &quot;magic&quot;:

(roughly translated to your names in the user model)

def linked
    self.linked_to &#124; self.linked_from
end]]></description>
		<content:encoded><![CDATA[<p>still very usefull. having mine half way done, i&#8217;ve found yours and it helped a lot to get mine fully done. i&#8217;ve solved(hopefully?) the bidirectional thing by using not rails but ruby &#8220;magic&#8221;:</p>
<p>(roughly translated to your names in the user model)</p>
<p>def linked<br />
    self.linked_to | self.linked_from<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: guillaume</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-650</link>
		<dc:creator><![CDATA[guillaume]]></dc:creator>
		<pubDate>Tue, 26 Aug 2008 21:01:34 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-650</guid>
		<description><![CDATA[Thanks a lot for your post, it helped me a lot !]]></description>
		<content:encoded><![CDATA[<p>Thanks a lot for your post, it helped me a lot !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: guillaume</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-649</link>
		<dc:creator><![CDATA[guillaume]]></dc:creator>
		<pubDate>Tue, 26 Aug 2008 20:56:42 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-649</guid>
		<description><![CDATA[Thanks a lot for your post]]></description>
		<content:encoded><![CDATA[<p>Thanks a lot for your post</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: szeryf</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-313</link>
		<dc:creator><![CDATA[szeryf]]></dc:creator>
		<pubDate>Fri, 23 Nov 2007 20:35:46 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-313</guid>
		<description><![CDATA[i didn&#039;t use join, so I&#039;m not sure which query you are referring to. your query returns the same as my query on view. nonetheless, I&#039;m glad this helped you :)]]></description>
		<content:encoded><![CDATA[<p>i didn&#8217;t use join, so I&#8217;m not sure which query you are referring to. your query returns the same as my query on view. nonetheless, I&#8217;m glad this helped you :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: provides 'brain'</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-312</link>
		<dc:creator><![CDATA[provides 'brain']]></dc:creator>
		<pubDate>Fri, 23 Nov 2007 17:26:49 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-312</guid>
		<description><![CDATA[great article, BTW, it really helped me a lot to understand rails relationships and self relationships and stuff. It worked great for a project in university.

thanks !]]></description>
		<content:encoded><![CDATA[<p>great article, BTW, it really helped me a lot to understand rails relationships and self relationships and stuff. It worked great for a project in university.</p>
<p>thanks !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: provides 'brain'</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-311</link>
		<dc:creator><![CDATA[provides 'brain']]></dc:creator>
		<pubDate>Fri, 23 Nov 2007 17:25:35 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-311</guid>
		<description><![CDATA[actually, it should be
 
select * from users where id in (select user_id from relationships where buddy_id = #{n} union select buddy_id from relationships where user_id = #{4});]]></description>
		<content:encoded><![CDATA[<p>actually, it should be</p>
<p>select * from users where id in (select user_id from relationships where buddy_id = #{n} union select buddy_id from relationships where user_id = #{4});</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: provides 'brain'</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-310</link>
		<dc:creator><![CDATA[provides 'brain']]></dc:creator>
		<pubDate>Fri, 23 Nov 2007 15:55:05 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-310</guid>
		<description><![CDATA[Hey, you&#039;r inner join doesn&#039;t work for the &#039;linked&#039; case, it just selects the &#039;linked_from&#039;.
art&#039;s solution should work.]]></description>
		<content:encoded><![CDATA[<p>Hey, you&#8217;r inner join doesn&#8217;t work for the &#8216;linked&#8217; case, it just selects the &#8216;linked_from&#8217;.<br />
art&#8217;s solution should work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: szeryf</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-241</link>
		<dc:creator><![CDATA[szeryf]]></dc:creator>
		<pubDate>Sat, 27 Oct 2007 07:59:25 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-241</guid>
		<description><![CDATA[Yes, you can add additional fields to the relations table, but you cannot access them like this. The linked_to collection is a collection of User object and they don&#039;t have feeling attribute. You have to use relation_to collection. Something like this:

@v.relation_to &lt;&lt; @w, :feeling =&gt; &#039;good&#039;
]]></description>
		<content:encoded><![CDATA[<p>Yes, you can add additional fields to the relations table, but you cannot access them like this. The linked_to collection is a collection of User object and they don&#8217;t have feeling attribute. You have to use relation_to collection. Something like this:</p>
<p>@v.relation_to &lt;&lt; @w, :feeling =&gt; &#8216;good&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Suryc</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-239</link>
		<dc:creator><![CDATA[Suryc]]></dc:creator>
		<pubDate>Fri, 26 Oct 2007 15:41:11 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-239</guid>
		<description><![CDATA[Hey, 
Is it possible :
 - to store an other field ?

For example :
create_table :relationships, :id =&gt; false do &#124;t&#124;
  t.column &quot;user_id&quot;,  :integer, :null =&gt; false
  t.column &quot;buddy_id&quot;, :integer, :null =&gt; false
  t.column &quot;feelling&quot;, : string
end

 - and to give the new paramter in :
@v.linked_to(feeling) &lt;&lt; @w

Thx]]></description>
		<content:encoded><![CDATA[<p>Hey,<br />
Is it possible :<br />
 &#8211; to store an other field ?</p>
<p>For example :<br />
create_table :relationships, :id =&gt; false do |t|<br />
  t.column &#8220;user_id&#8221;,  :integer, :null =&gt; false<br />
  t.column &#8220;buddy_id&#8221;, :integer, :null =&gt; false<br />
  t.column &#8220;feelling&#8221;, : string<br />
end</p>
<p> &#8211; and to give the new paramter in :<br />
@v.linked_to(feeling) &lt;&lt; @w</p>
<p>Thx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: &#8220;Self-referential many-to-many relations in Ruby on Rails&#8221; - Art at Code</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-8</link>
		<dc:creator><![CDATA[&#8220;Self-referential many-to-many relations in Ruby on Rails&#8221; - Art at Code]]></dc:creator>
		<pubDate>Fri, 13 Jul 2007 12:34:35 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-8</guid>
		<description><![CDATA[[...] data makes me crazy I went on searching for any helpful information and found szeryf&#8217;s article. He uses some custom SQL to avoid [...]]]></description>
		<content:encoded><![CDATA[<p>[...] data makes me crazy I went on searching for any helpful information and found szeryf&#8217;s article. He uses some custom SQL to avoid [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: szeryf</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-6</link>
		<dc:creator><![CDATA[szeryf]]></dc:creator>
		<pubDate>Mon, 02 Jul 2007 18:04:31 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-6</guid>
		<description><![CDATA[art: yes and no :-) on the lower level we want to have directed relations in case we need to know who started the relation etc. but there are other situations when we want to treat the relationships as undirected (looking for a path between two users). that&#039;s why I defined two versions of the relations. 

as of your version of finding friends, I think it&#039;s equivalent to my version. the difference is that you don&#039;t use a view and instead do the union-trick inline in your query.

PS. thanks for first comment on my blog :)]]></description>
		<content:encoded><![CDATA[<p>art: yes and no :-) on the lower level we want to have directed relations in case we need to know who started the relation etc. but there are other situations when we want to treat the relationships as undirected (looking for a path between two users). that&#8217;s why I defined two versions of the relations. </p>
<p>as of your version of finding friends, I think it&#8217;s equivalent to my version. the difference is that you don&#8217;t use a view and instead do the union-trick inline in your query.</p>
<p>PS. thanks for first comment on my blog :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: art</title>
		<link>http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-5</link>
		<dc:creator><![CDATA[art]]></dc:creator>
		<pubDate>Mon, 02 Jul 2007 16:28:06 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2007/06/27/self-referential-many-to-many-relations-in-ruby-on-rails/#comment-5</guid>
		<description><![CDATA[I was just wondering...
aren&#039;t the lists all the same?:
   1. a list of users directly linked to (me → someone)
   2. a list of users directly linked from (someone → me)
   3. a list of users directly linked, no matter to or from (e.g. if A → B and B → C, then A and C should be on B’s linked list)

If this is so, you only need a find_friends method in your user model:
def self.find_friends(user_id)
    find_by_sql(&quot;SELECT users.* 
                 FROM (
                   SELECT user_id, friend_id
                   FROM friendships
                   WHERE user_id = #{user_id}
                   UNION
                   SELECT friend_id, user_id
                   FROM friendships
                   WHERE friend_id = #{user_id}
                ) AS friends
                INNER JOIN users
                ON friends.friend_id = users.id
              &quot;)
  end

please correct me if I&#039;m wrong :)]]></description>
		<content:encoded><![CDATA[<p>I was just wondering&#8230;<br />
aren&#8217;t the lists all the same?:<br />
   1. a list of users directly linked to (me → someone)<br />
   2. a list of users directly linked from (someone → me)<br />
   3. a list of users directly linked, no matter to or from (e.g. if A → B and B → C, then A and C should be on B’s linked list)</p>
<p>If this is so, you only need a find_friends method in your user model:<br />
def self.find_friends(user_id)<br />
    find_by_sql(&#8220;SELECT users.*<br />
                 FROM (<br />
                   SELECT user_id, friend_id<br />
                   FROM friendships<br />
                   WHERE user_id = #{user_id}<br />
                   UNION<br />
                   SELECT friend_id, user_id<br />
                   FROM friendships<br />
                   WHERE friend_id = #{user_id}<br />
                ) AS friends<br />
                INNER JOIN users<br />
                ON friends.friend_id = users.id<br />
              &#8220;)<br />
  end</p>
<p>please correct me if I&#8217;m wrong :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
