<?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>has_many :follows</title>
	<atom:link href="http://hasmanyfollows.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://hasmanyfollows.com</link>
	<description>bits of rails code for social apps</description>
	<lastBuildDate>Mon, 14 Sep 2009 23:28:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How To Setup Twitter Style Following In Your User Model</title>
		<link>http://hasmanyfollows.com/2009/09/12/how-to-setup-twitter-style-following-in-your-user-model/</link>
		<comments>http://hasmanyfollows.com/2009/09/12/how-to-setup-twitter-style-following-in-your-user-model/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 23:31:56 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[follow]]></category>
		<category><![CDATA[self-referential]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://hasmanyfollows.com/?p=18</guid>
		<description><![CDATA[Setting up a Twitter-like following scheme can be a little tricky at first glance. The reason being that your User model will need to refer to itself in order for a user to follow another user. Here&#8217;s how to look at the relationships:

We can build this relationship using self-referential associations, and a model as a [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up a Twitter-like following scheme can be a little tricky at first glance. The reason being that your User model will need to refer to itself in order for a user to follow another user. Here&#8217;s how to look at the relationships:</p>
<p><img class="aligncenter size-full wp-image-20" title="user-follows" src="http://hasmanyfollows.com/wp-content/uploads/2009/09/user-follows.gif" alt="user-follows" width="262" height="135" /></p>
<p>We can build this relationship using self-referential associations, and a model as a join table. We&#8217;ll call the joining model Follow.</p>
<div class="filepath">db/migrate/create_follows.rb</div>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateFollows <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:follows</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#9900CC;">integer</span> <span style="color:#ff3333; font-weight:bold;">:follower_id</span>
      t.<span style="color:#9900CC;">integer</span> <span style="color:#ff3333; font-weight:bold;">:followed_id</span>
      ...</pre></div></div>

<p>Next, we can add the follower and followed associations to the our new Follow model.</p>
<div class="filepath">app/models/follow.rb</div>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Follow <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:follower</span>, <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;User&quot;</span>
  <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:followed</span>, <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;User&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Lastly, we can set up the self-referential associations in the User model.</p>
<div class="filepath">app/models/user.rb</div>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:follows</span>, <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;follower_id&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Follow&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:dependent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:destroy</span>
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:users_followed</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:follows</span>, <span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:followed</span>
&nbsp;
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:followings</span>, <span style="color:#ff3333; font-weight:bold;">:foreign_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;followed_id&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:class_name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Follow&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:dependent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:destroy</span>
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:users_following</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:followings</span>, <span style="color:#ff3333; font-weight:bold;">:source</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:follower</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This allows the User to be both followed and a follower. See <a title="Self-referential has_many :through associations" href="http://blog.hasmanythrough.com/2007/10/30/self-referential-has-many-through" target="_blank">this blog post</a> for a good explanation of self-referential associations.</p>
<p>Your controller can look basically like this:</p>
<div class="filepath">app/controllers/follows_controller.rb</div>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FollowsController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@follow</span> = current_user.<span style="color:#9900CC;">follows</span>.<span style="color:#9900CC;">build</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:followed_id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:followed_id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@follow</span>.<span style="color:#5A0A0A; font-weight:bold;">save</span>
      <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You are now following #{@follow.followed.name}&quot;</span>
      <span style="color:#5A0A0A; font-weight:bold;">redirect_to</span> user_path<span style="color:#006600; font-weight:bold;">&#40;</span>@follow.<span style="color:#9900CC;">followed</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Unable to follow.&quot;</span>
      <span style="color:#5A0A0A; font-weight:bold;">redirect_to</span> user_path<span style="color:#006600; font-weight:bold;">&#40;</span>@follow.<span style="color:#9900CC;">followed</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#5A0A0A; font-weight:bold;">destroy</span>
    <span style="color:#0066ff; font-weight:bold;">@follow</span> = current_user.<span style="color:#9900CC;">follows</span>.<span style="color:#9900CC;">find_by_followed_id</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:followed_id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@follow</span>.<span style="color:#5A0A0A; font-weight:bold;">destroy</span>
    <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;Removed follow.&quot;</span>
    <span style="color:#5A0A0A; font-weight:bold;">redirect_to</span> user_path<span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:followed_id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://hasmanyfollows.com/2009/09/12/how-to-setup-twitter-style-following-in-your-user-model/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting Cucumber, Authlogic, and Machinist to Play Together</title>
		<link>http://hasmanyfollows.com/2009/09/06/getting-cucumber-authlogic-and-machinist-to-play-together/</link>
		<comments>http://hasmanyfollows.com/2009/09/06/getting-cucumber-authlogic-and-machinist-to-play-together/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 09:15:24 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[machinist]]></category>

		<guid isPermaLink="false">http://hasmanyfollows.com/?p=3</guid>
		<description><![CDATA[Out of the box, Cucumber, Authlogic, and Machinist need a little bit of bridging to get them to work together. This post will cover setting up your Machinist blueprints for your User model, and Cucumber login stories and login steps. It also covers how to use Authlogic&#8217;s current_user method in your Cucumber stories.
This post assumes [...]]]></description>
			<content:encoded><![CDATA[<p>Out of the box, Cucumber, Authlogic, and Machinist need a little bit of bridging to get them to work together. This post will cover setting up your Machinist blueprints for your User model, and Cucumber login stories and login steps. It also covers how to use Authlogic&#8217;s current_user method in your Cucumber stories.</p>
<p>This post assumes you already have Cucumber, Authlogic and Machinist setup and bootstrapped. If not, see these links first: <a title="Install Cucumber, Webrat and RSpec" href="http://wiki.github.com/aslakhellesoy/cucumber/ruby-on-rails" target="_blank">Cucumber</a>, <a title="Authlogic setup tutorial" href="http://github.com/binarylogic/authlogic_example" target="_blank">Authlogic</a>, <a title="Machinist install and setup" href="http://github.com/notahat/machinist" target="_blank">Machinist</a>. </p>
<p>When writing your Cucumber stories you will inevitably come across scenarios like the following:</p>
<div class="filepath">features/user.feature</div>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Scenario: User fills in profile
   Given I am a logged in user
   And I am on the My Profile page
   ...</pre></div></div>

<p>How do you setup a logged in user? First, you need create a Machinist blueprint for your user. </p>
<div class="filepath">spec/blueprint.rb</div>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">blueprint</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  username <span style="color:#996600;">'test_user'</span>
  email <span style="color:#996600;">'test@user.com'</span>
  password <span style="color:#996600;">'spacemonkey'</span>
  password_confirmation <span style="color:#996600;">'spacemonkey'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Next you will need to create step definitions for your login stories. Here are a few handy functions that can make the step of creating a logged in user easier and more module:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> user
   <span style="color:#0066ff; font-weight:bold;">@user</span> <span style="color:#006600; font-weight:bold;">||</span>= User.<span style="color:#9900CC;">make</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> login
  user
  visit <span style="color:#996600;">'/login'</span>
  fill_in<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Email&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">email</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  fill_in<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Password&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">password</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  click_button<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Login&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Next we can create the login step:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>^I am a logged <span style="color:#9966CC; font-weight:bold;">in</span> user$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span>
   login
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Now the <code>Given I am a logged in user</code> step should pass. </p>
<p>In your other Cucumber step definitions it will come in handy to be able to use Authlogic&#8217;s built in <code>current_user</code> method in your Cucumber steps. Since the <code>current_user</code> method apply to users, you can add it to your <code>user_steps.rb</code> file within a module:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> UserHelpers
  <span style="color:#9966CC; font-weight:bold;">def</span> current_user_session
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@current_user_session</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@current_user_session<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@current_user_session</span> = UserSession.<span style="color:#9900CC;">find</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> current_user
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@current_user</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>@current_user<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@current_user</span> = current_user_session <span style="color:#006600; font-weight:bold;">&amp;</span>amp;<span style="color:#006600; font-weight:bold;">&amp;</span>amp; current_user_session.<span style="color:#9900CC;">user</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
World<span style="color:#006600; font-weight:bold;">&#40;</span>UserHelpers<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Now you should be able to easily run tests involving the logged in session. For example, to make the <code>And I am on the My Profile page</code> step pass you will have to add the path to your <code>path.rb</code> file. In the path reference we can now make use of Authlogic&#8217;s <code>current_user</code> method:</p>
<div class="filepath">features/support/paths.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> NavigationHelpers
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> path_to<span style="color:#006600; font-weight:bold;">&#40;</span>page_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> page_name
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>the My Profile page<span style="color:#006600; font-weight:bold;">/</span>
      user_path<span style="color:#006600; font-weight:bold;">&#40;</span>current_user<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
World<span style="color:#006600; font-weight:bold;">&#40;</span>NavigationHelpers<span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Another useful step to add to your user steps is one which creates multiple users. The following step can be used to make a specific amount of users:</p>
<div class="filepath">features/step_definitions/user_steps.rb</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>15
16
17
18
19
20
</pre></td><td class="code"><pre class="rails" style="font-family:monospace;">Given <span style="color:#006600; font-weight:bold;">/</span>^there exists at least <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span> other user$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>number<span style="color:#006600; font-weight:bold;">|</span>
   <span style="color:#0066ff; font-weight:bold;">@users</span> = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
   number.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\d<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">join</span>.<span style="color:#5A0A0A; font-weight:bold;">to_i</span>.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>i<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#0066ff; font-weight:bold;">@users</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = User.<span style="color:#9900CC;">make</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>In the end your <code>user_steps.rb</code> file should look like:<br />
<script src="http://gist.github.com/186744.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://hasmanyfollows.com/2009/09/06/getting-cucumber-authlogic-and-machinist-to-play-together/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
