<?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>Fri, 16 Dec 2011 08:18:27 +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>Backbone.js and Rails has_many :through Relationships</title>
		<link>http://hasmanyfollows.com/2011/12/08/backbone-js-and-rails-has_many-through-relationships/</link>
		<comments>http://hasmanyfollows.com/2011/12/08/backbone-js-and-rails-has_many-through-relationships/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 22:31:12 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Backbone]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://hasmanyfollows.com/?p=116</guid>
		<description><![CDATA[How to implement a many-to-many relationship in backbone.js that corresponds to a Rails has_many :through relationship.]]></description>
			<content:encoded><![CDATA[<p>The has_many :through relationship is a core relationship for most rails apps. Implementing a Backbone front-end into your app requires mirroring your many-to-many back-end model relationship in Backbone. While <a href="https://github.com/PaulUithol/Backbone-relational" title="Backbone-relational">Backbone-relational</a> provides a great framework for handling model relationships in Backbone, I wanted to see if I could get it working on my own with only the bare, necessary functionality.</p>
<p>First, the Rails models. I will use a typical shopping cart example:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Order <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:line_items</span>
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:products</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;">:line_items</span>
  accepts_nested_attributes_for <span style="color:#ff3333; font-weight:bold;">:line_items</span>, <span style="color:#ff3333; font-weight:bold;">:allow_destroy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Prodcts <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> LineItem <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
  <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:order</span>
  <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:product</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>So an Order has many Products through the LineItem join model. In this many-to-many scenario I only care about the Products an Order has, and do not need to know the inverse–what Orders a specific Product has been in. For this reason, I am not setting up any relationships in the Product model.</p>
<p>Now to set up the Backbone model structure. In Backbone, the model relationships will not be a 1-to-1 mapping of the rails model relationships. In Backbone we simply need to worry about an Order containing a collection of Products. No need for a join model. See this <a href="http://stackoverflow.com/a/6669866">post</a> for a more detailed explanation.</p>
<p>Backbone models (in CoffeeScript):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Product extends Backbone.<span style="color:#9900CC;">Model</span>
  paramRoot: <span style="color:#996600;">'region'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ProductsCollection extends Backbone.<span style="color:#9900CC;">Collection</span>
  model: Product
  url: <span style="color:#996600;">'/products'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Order extends Backbone.<span style="color:#9900CC;">Model</span>
  initialize: <span style="color:#006600; font-weight:bold;">-&gt;</span>
    <span style="color:#0066ff; font-weight:bold;">@products</span> = <span style="color:#5A0A0A; font-weight:bold;">new</span> ProductsCollection<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  line_items_attributes: <span style="color:#006600; font-weight:bold;">-&gt;</span>
    <span style="color:#0066ff; font-weight:bold;">@products</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
      <span style="color:#006600; font-weight:bold;">&#123;</span>product_id: <span style="color:#CC0066; font-weight:bold;">p</span>.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;id&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  toJSON: <span style="color:#006600; font-weight:bold;">-&gt;</span>
    json = <span style="color:#006600; font-weight:bold;">&#123;</span>order : _.<span style="color:#5A0A0A; font-weight:bold;">clone</span><span style="color:#006600; font-weight:bold;">&#40;</span>@attributes<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
    _.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>json.<span style="color:#9900CC;">order</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>line_items_attributes: <span style="color:#0066ff; font-weight:bold;">@line_items_attributes</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Two important points here. Our Order instances initialize to have an attribute @products, which is just a Backbone Collection to hold all of its related products. This will emulate a one-to-many relationship.</p>
<p>Second, we are overwriting toJSON() in order to structure the JSON data so that it&#8217;s useful to rails. More on that later.</p>
<p>Next let&#8217;s put the Backbone models to use. I can add Products to an Order using the add() funciton.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@order</span> = <span style="color:#5A0A0A; font-weight:bold;">new</span> Order<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@order</span>.<span style="color:#9900CC;">products</span>.<span style="color:#5A0A0A; font-weight:bold;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>@product1, <span style="color:#0066ff; font-weight:bold;">@product2</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;">@order</span>.<span style="color:#5A0A0A; font-weight:bold;">save</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>When the order is saved, the overwritten toJSON() function will also include the order&#8217;s product data. The JSON data needs to be structured in such a way as to tell rails to create a new Order record, and new LineItem records for each product in the order.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;order&quot;</span><span style="color: #339933;">=&gt;</span>
  <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;user_id&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #CC0000;">99</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;line_items_attributes&quot;</span><span style="color: #339933;">=&gt;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;product_id&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;product_id&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #CC0000;">11</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;product_id&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #CC0000;">26</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Setting</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">accepts_nested_attributes_for <span style="color:#ff3333; font-weight:bold;">:line_items</span></pre></div></div>

<p> in the Order moddel allows it to create both Order records and associated LineItem records in one JSON post.</p>
<p>Since rails only needs to know the ids of each Product we wish to associate to an Order, the products.add() call can be simplified to only contain id data.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@order</span> = <span style="color:#5A0A0A; font-weight:bold;">new</span> Order<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@order</span>.<span style="color:#9900CC;">products</span>.<span style="color:#5A0A0A; font-weight:bold;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#123;</span>product_id: <span style="color:#0066ff; font-weight:bold;">@product1</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>product_id: <span style="color:#0066ff; font-weight:bold;">@product2</span>.<span style="color:#9900CC;">id</span><span style="color:#006600; font-weight:bold;">&#125;</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;">@order</span>.<span style="color:#5A0A0A; font-weight:bold;">save</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This will ensure our JSON data is as clean and simple as it needs to be in order to create the association records. </p>
<p>This is a very rough first pass at getting Backbone to play well with a Rails has_many :through set-up in one direction. I haven&#8217;t covered how to load existing Orders into Backbone for view rendering. This will require customizing the as_json method in rails to include it&#8217;s related Product data. I hope to cover that in a part 2 post. Please fill me in on other approaches you have come up with to handle Backbone model associations.</p>
]]></content:encoded>
			<wfw:commentRss>http://hasmanyfollows.com/2011/12/08/backbone-js-and-rails-has_many-through-relationships/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Extracting Geolocation Image Data with Carrierwave and RMagick on Heroku</title>
		<link>http://hasmanyfollows.com/2011/06/03/extracting-geolocation-image-data-with-carrierwave-and-rmagick-on-heroku/</link>
		<comments>http://hasmanyfollows.com/2011/06/03/extracting-geolocation-image-data-with-carrierwave-and-rmagick-on-heroku/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 20:51:22 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[CarrierWave]]></category>
		<category><![CDATA[exif]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://hasmanyfollows.com/?p=96</guid>
		<description><![CDATA[Many smartphones embed latitude and longitude EXIF data relative to where the photo was taken. So why not extract that data and store it to the database when a user uploads an image? I am currently using this technique in an API I built which allows users to post a picture of an item they [...]]]></description>
			<content:encoded><![CDATA[<p>Many smartphones embed latitude and longitude <a href="http://en.wikipedia.org/wiki/Exchangeable_image_file_format">EXIF</a> data relative to where the photo was taken. So why not extract that data and store it to the database when a user uploads an image?</p>
<p>I am currently using this technique in an API I built which allows users to post a picture of an item they would like to sell. Next an ad is created for the item with the location populated by the image&#8217;s embedded geolocation data.</p>
<p>On Heroku we would like the exif data extracted from the temporary image file before it is uploaded to S3. To do this we can call a method to extract the exif date before_save. In the model file:</p>
<div class="filepath">app/models/item_image.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> ItemImage <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
  <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:item</span>
&nbsp;
  mount_uploader <span style="color:#ff3333; font-weight:bold;">:image</span>, ImageUploader
&nbsp;
  <span style="color:#5A0A0A; font-weight:bold;">before_save</span> <span style="color:#ff3333; font-weight:bold;">:extract_geolocation</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> extract_geolocation
    img = <span style="color:#6666ff; font-weight:bold;">Magick::Image</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#40;</span>image<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">unless</span> img
    img_lat = img.<span style="color:#9900CC;">get_exif_by_entry</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'GPSLatitude'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">', '</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
    img_lng = img.<span style="color:#9900CC;">get_exif_by_entry</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'GPSLongitude'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">', '</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
    lat_ref = img.<span style="color:#9900CC;">get_exif_by_entry</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'GPSLatitudeRef'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
    lng_ref = img.<span style="color:#9900CC;">get_exif_by_entry</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'GPSLongitudeRef'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">unless</span> img_lat <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> img_lng <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> lat_ref <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> lng_ref
&nbsp;
    latitude = to_frac<span style="color:#006600; font-weight:bold;">&#40;</span>img_lat<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span>to_frac<span style="color:#006600; font-weight:bold;">&#40;</span>img_lat<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">60</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span>to_frac<span style="color:#006600; font-weight:bold;">&#40;</span>img_lat<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3600</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    longitude = to_frac<span style="color:#006600; font-weight:bold;">&#40;</span>img_lng<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span>to_frac<span style="color:#006600; font-weight:bold;">&#40;</span>img_lng<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">60</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span>to_frac<span style="color:#006600; font-weight:bold;">&#40;</span>img_lng<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">3600</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    latitude = latitude <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> lat_ref == <span style="color:#996600;">'S'</span>  <span style="color:#008000; font-style:italic;"># (N is +, S is -)</span>
    longitude = longitude <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">if</span> lng_ref == <span style="color:#996600;">'W'</span>   <span style="color:#008000; font-style:italic;"># (W is -, E is +)</span>
&nbsp;
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">lat</span> = latitude
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">lng</span> = longitude
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> to_frac<span style="color:#006600; font-weight:bold;">&#40;</span>strng<span style="color:#006600; font-weight:bold;">&#41;</span>
    numerator, denominator = strng.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:to_f<span style="color:#006600; font-weight:bold;">&#41;</span>
    denominator <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006666;">1</span>
    numerator<span style="color:#006600; font-weight:bold;">/</span>denominator
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The extract_geolocation method uses RMagick to access the image EXIF data. If it contains latitude and longitude data expressed as linear units (degree, minute, second), but we would like it in decimal format so that it can be fed into the <a href="http://code.google.com/apis/maps/documentation/webservices/index.html">Google Maps API</a> and returned as a city, state and zipcode. This is what the calculations in the method are handling.</p>
<p>With a gem like <a href="http://www.rubygeocoder.com/">Geocoder</a> we can easily wire in geocode look up and store the city, state, and zipcode in the image record.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">if</span> geo = Geocoder.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{latitude},#{longitude}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#5A0A0A; font-weight:bold;">first</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">city</span> = geo.<span style="color:#9900CC;">city</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">state</span> = geo.<span style="color:#9900CC;">state</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">zipcode</span> = geo.<span style="color:#9900CC;">postal_code</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Putting it all together:<br />
<script src="http://gist.github.com/1007160.js"></script></p>
<p>Feedback on how to improve the process is welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://hasmanyfollows.com/2011/06/03/extracting-geolocation-image-data-with-carrierwave-and-rmagick-on-heroku/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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 [...]]]></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:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span></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:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></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:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></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:#5A0A0A; 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:#5A0A0A; 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 [...]]]></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>

