<?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/"
		>
<channel>
	<title>Comments on: Has Many has_many: A Refactoring Story</title>
	<atom:link href="http://pathfindersoftware.com/2008/11/has-many-has_many-a-refactoring-story/feed/" rel="self" type="application/rss+xml" />
	<link>http://pathfindersoftware.com/2008/11/has-many-has_many-a-refactoring-story/</link>
	<description>The Fastest Way to Launch Successful Software</description>
	<lastBuildDate>Thu, 19 Jan 2012 16:36:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Vishu</title>
		<link>http://pathfindersoftware.com/2008/11/has-many-has_many-a-refactoring-story/#comment-8998</link>
		<dc:creator>Vishu</dc:creator>
		<pubDate>Mon, 24 Nov 2008 21:07:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.pathf.com/blogs/?p=1260#comment-8998</guid>
		<description>That says


class Story &lt; ActiveRecord::Base
  acts_as_collectible

  named_scope :uncompleted, :conditions =&gt; {:complete =&gt; false}
end


I think &lt; got interpreted as a tag.</description>
		<content:encoded><![CDATA[<p>That says</p>
<p>class Story &lt; ActiveRecord::Base<br />
  acts_as_collectible</p>
<p>  named_scope :uncompleted, :conditions =&gt; {:complete =&gt; false}<br />
end</p>
<p>I think &lt; got interpreted as a tag.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vishu</title>
		<link>http://pathfindersoftware.com/2008/11/has-many-has_many-a-refactoring-story/#comment-8997</link>
		<dc:creator>Vishu</dc:creator>
		<pubDate>Mon, 24 Nov 2008 20:56:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.pathf.com/blogs/?p=1260#comment-8997</guid>
		<description>I agree that it feels weird to put the collection operations on the story class. It&#039;s just that that seems to be the intended rails way.

I also might prefer to put the collection operations on a pluralized class. Something like:

module Stories
  def remaining_uncompleted_hours
    uncompleted.sum(:hours)
  end
end

I played around with some implementation options and the best I could come up with was:

class Story  {:complete =&gt; false}
end

With a plugin :

module ActsAsCollectible
  def self.included(base) # :nodoc:
    base.extend ClassMethods
  end

  module ClassMethods
    def acts_as_collectible
      # determine the plural using pluralize
      # todo: take the name of the module as option
      self.extend self.name.pluralize.constantize
    end
  end
end</description>
		<content:encoded><![CDATA[<p>I agree that it feels weird to put the collection operations on the story class. It&#8217;s just that that seems to be the intended rails way.</p>
<p>I also might prefer to put the collection operations on a pluralized class. Something like:</p>
<p>module Stories<br />
  def remaining_uncompleted_hours<br />
    uncompleted.sum(:hours)<br />
  end<br />
end</p>
<p>I played around with some implementation options and the best I could come up with was:</p>
<p>class Story  {:complete =&gt; false}<br />
end</p>
<p>With a plugin :</p>
<p>module ActsAsCollectible<br />
  def self.included(base) # :nodoc:<br />
    base.extend ClassMethods<br />
  end</p>
<p>  module ClassMethods<br />
    def acts_as_collectible<br />
      # determine the plural using pluralize<br />
      # todo: take the name of the module as option<br />
      self.extend self.name.pluralize.constantize<br />
    end<br />
  end<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vishu</title>
		<link>http://pathfindersoftware.com/2008/11/has-many-has_many-a-refactoring-story/#comment-8996</link>
		<dc:creator>Vishu</dc:creator>
		<pubDate>Fri, 21 Nov 2008 19:52:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.pathf.com/blogs/?p=1260#comment-8996</guid>
		<description>Yikes - I&#039;d love a do-over on the formatting there! Feel free to contact me. I can just send you the code or try again with a little more formatting instruction.</description>
		<content:encoded><![CDATA[<p>Yikes &#8211; I&#8217;d love a do-over on the formatting there! Feel free to contact me. I can just send you the code or try again with a little more formatting instruction.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vishu</title>
		<link>http://pathfindersoftware.com/2008/11/has-many-has_many-a-refactoring-story/#comment-8995</link>
		<dc:creator>Vishu</dc:creator>
		<pubDate>Fri, 21 Nov 2008 19:47:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.pathf.com/blogs/?p=1260#comment-8995</guid>
		<description>The general problem is what to do when you have a collection of something and you want to have operations on the collection. Rails says: &quot;put the collection operations on the class&quot; and makes the class methods available (with the correct scope) through the access methods.

So, create Story.remaining_uncompleted_hours and call it through the stories collection.

Here&#039;s how I would do it (with a simplified implementation):

Here&#039;s the test. I&#039;m creating a couple of users to demonstrate that the scoping is working

class UserTest  true,  :hours =&gt; 2
    joe.stories.create :complete =&gt; false, :hours =&gt; 2
    joe.stories.create :complete =&gt; false, :hours =&gt; 2

    frank = User.create
    frank.stories.create :complete =&gt; true,  :hours =&gt; 2
    frank.stories.create :complete =&gt; false, :hours =&gt; 2
    frank.stories.create :complete =&gt; false, :hours =&gt; 2

    ################################################
    # this is how calls through the attribute look
    assert_equal 4, joe.stories.remaining_uncompleted_hours
  end
end

Story has a class method named remaining_uncompleted_hours
class Story  {:complete =&gt; false}

    ################################################
    # here&#039;s where I define the class method.
    def Story.remaining_uncompleted_hours
      uncompleted.sum(:hours)
    end
end

The simplified schema
ActiveRecord::Schema.define(:version =&gt; 20081121185349) do

  create_table &quot;stories&quot;, :force =&gt; true do &#124;t&#124;
    t.integer  &quot;user_id&quot;
    t.boolean  &quot;complete&quot;
    t.datetime &quot;created_at&quot;
    t.datetime &quot;updated_at&quot;
    t.integer  &quot;hours&quot;
  end

  create_table &quot;users&quot;, :force =&gt; true do &#124;t&#124;
    t.datetime &quot;created_at&quot;
    t.datetime &quot;updated_at&quot;
  end

end

A super-simple user model
class User &lt; ActiveRecord::Base
    has_many :stories
end</description>
		<content:encoded><![CDATA[<p>The general problem is what to do when you have a collection of something and you want to have operations on the collection. Rails says: &#8220;put the collection operations on the class&#8221; and makes the class methods available (with the correct scope) through the access methods.</p>
<p>So, create Story.remaining_uncompleted_hours and call it through the stories collection.</p>
<p>Here&#8217;s how I would do it (with a simplified implementation):</p>
<p>Here&#8217;s the test. I&#8217;m creating a couple of users to demonstrate that the scoping is working</p>
<p>class UserTest  true,  :hours =&gt; 2<br />
    joe.stories.create :complete =&gt; false, :hours =&gt; 2<br />
    joe.stories.create :complete =&gt; false, :hours =&gt; 2</p>
<p>    frank = User.create<br />
    frank.stories.create :complete =&gt; true,  :hours =&gt; 2<br />
    frank.stories.create :complete =&gt; false, :hours =&gt; 2<br />
    frank.stories.create :complete =&gt; false, :hours =&gt; 2</p>
<p>    ################################################<br />
    # this is how calls through the attribute look<br />
    assert_equal 4, joe.stories.remaining_uncompleted_hours<br />
  end<br />
end</p>
<p>Story has a class method named remaining_uncompleted_hours<br />
class Story  {:complete =&gt; false}</p>
<p>    ################################################<br />
    # here&#8217;s where I define the class method.<br />
    def Story.remaining_uncompleted_hours<br />
      uncompleted.sum(:hours)<br />
    end<br />
end</p>
<p>The simplified schema<br />
ActiveRecord::Schema.define(:version =&gt; 20081121185349) do</p>
<p>  create_table &#8220;stories&#8221;, :force =&gt; true do |t|<br />
    t.integer  &#8220;user_id&#8221;<br />
    t.boolean  &#8220;complete&#8221;<br />
    t.datetime &#8220;created_at&#8221;<br />
    t.datetime &#8220;updated_at&#8221;<br />
    t.integer  &#8220;hours&#8221;<br />
  end</p>
<p>  create_table &#8220;users&#8221;, :force =&gt; true do |t|<br />
    t.datetime &#8220;created_at&#8221;<br />
    t.datetime &#8220;updated_at&#8221;<br />
  end</p>
<p>end</p>
<p>A super-simple user model<br />
class User &lt; ActiveRecord::Base<br />
    has_many :stories<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Piotr Sarnacki</title>
		<link>http://pathfindersoftware.com/2008/11/has-many-has_many-a-refactoring-story/#comment-8994</link>
		<dc:creator>Piotr Sarnacki</dc:creator>
		<pubDate>Sat, 15 Nov 2008 11:07:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.pathf.com/blogs/?p=1260#comment-8994</guid>
		<description>Great refactoring story :)

I love refactoring my old code and seeing progress I made.

Only one thing I would change - I would rather not place this kind of modules in initializers. If one of the gems fails initializers won&#039;t be run and you will end up with en error.

Cheers</description>
		<content:encoded><![CDATA[<p>Great refactoring story <img src='http://pathfindersoftware.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I love refactoring my old code and seeing progress I made.</p>
<p>Only one thing I would change &#8211; I would rather not place this kind of modules in initializers. If one of the gems fails initializers won&#8217;t be run and you will end up with en error.</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (User agent is rejected)
Page Caching using memcached (User agent is rejected)

Served from: pathfindersoftware.com @ 2012-02-10 01:54:55 -->
