<?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 with arguments</title>
	<atom:link href="http://www.dweebd.com/ruby/has_many-with-arguments/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dweebd.com/ruby/has_many-with-arguments/</link>
	<description>Web Freshness</description>
	<lastBuildDate>Mon, 05 Jul 2010 23:44:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: duncanbeevers</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-49</link>
		<dc:creator>duncanbeevers</dc:creator>
		<pubDate>Wed, 23 Apr 2008 20:52:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-49</guid>
		<description>ivo, if you read the blog post you&#039;ll see that the use of single-quotes in the conditions is what allows this to work.

More importantly, has_finder / named_scope provides this functionality in a more flexible way, and is committed to core Rails.  So please, use it instead!</description>
		<content:encoded><![CDATA[<p>ivo, if you read the blog post you&#8217;ll see that the use of single-quotes in the conditions is what allows this to work.</p>
<p>More importantly, has_finder / named_scope provides this functionality in a more flexible way, and is committed to core Rails.  So please, use it instead!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ivo</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-48</link>
		<dc:creator>ivo</dc:creator>
		<pubDate>Wed, 23 Apr 2008 13:11:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-48</guid>
		<description>&lt;code lang=&quot;ruby&quot;&gt;
class User
  has_many_with_args :whispers_from, :sender,
    :class_name =&gt; &#039;Whisper&#039;,
    :foreign_key =&gt; &#039;recipient_id&#039;,
    :conditions =&gt; &#039;sender_id = #{sender.id}&#039;
end
&lt;/code&gt;

must be

&lt;code lang=&quot;ruby&quot; class=&quot;donot&quot;&gt;
class User
  has_many_with_args :whispers_from, :sender,
    :class_name =&gt; &#039;Whisper&#039;,
    :foreign_key =&gt; &#039;recipient_id&#039;,
    :conditions =&gt; &quot;sender_id = #{sender.id}&quot;
end
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><pre class="ruby"><span class="kw1">class</span> User
  has_many_with_args <span class="re3">:whispers_from</span>, <span class="re3">:sender</span>,
    <span class="re3">:class_name</span> <span class="sy0">=&gt;</span> <span class="st0">'Whisper'</span>,
    <span class="re3">:foreign_key</span> <span class="sy0">=&gt;</span> <span class="st0">'recipient_id'</span>,
    <span class="re3">:conditions</span> <span class="sy0">=&gt;</span> <span class="st0">'sender_id = #{sender.id}'</span>
<span class="kw1">end</span></pre></p>
<p>must be</p>
<p><pre class="ruby donot"><span class="kw1">class</span> User
  has_many_with_args <span class="re3">:whispers_from</span>, <span class="re3">:sender</span>,
    <span class="re3">:class_name</span> <span class="sy0">=&gt;</span> <span class="st0">'Whisper'</span>,
    <span class="re3">:foreign_key</span> <span class="sy0">=&gt;</span> <span class="st0">'recipient_id'</span>,
    <span class="re3">:conditions</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;sender_id = #{sender.id}&quot;</span>
<span class="kw1">end</span></pre></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick Kallen</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-24</link>
		<dc:creator>Nick Kallen</dc:creator>
		<pubDate>Tue, 29 Jan 2008 06:15:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-24</guid>
		<description>I&#039;ve been told Scope_out works with will_paginate now (has_finder always has).</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been told Scope_out works with will_paginate now (has_finder always has).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kamal Fariz</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-23</link>
		<dc:creator>Kamal Fariz</dc:creator>
		<pubDate>Mon, 28 Jan 2008 23:27:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-23</guid>
		<description>Thanks Nick for the in-depth walkthrough of the features and differences to scope_out. Previously I had issues using scope_out with will_paginate where the options were not merged in paginate&#039;s find call. I&#039;m going to try replacing that with has_finder and give it a spin.</description>
		<content:encoded><![CDATA[<p>Thanks Nick for the in-depth walkthrough of the features and differences to scope_out. Previously I had issues using scope_out with will_paginate where the options were not merged in paginate&#8217;s find call. I&#8217;m going to try replacing that with has_finder and give it a spin.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick Kallen</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-22</link>
		<dc:creator>Nick Kallen</dc:creator>
		<pubDate>Mon, 28 Jan 2008 20:51:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-22</guid>
		<description>Hi, I&#039;m the author of has_finder. It&#039;s much better than scope_out. Scope_out has an old-fashioned API, using nested blocks rather than method chaining:

Person.greeks {  find(:all) } vs. Person.greeks.find(:all)

Scope_out doesn&#039;t allow composition without a sep declaration, vs. w/ HasFinder it comes for free:

combined_scope :greek_and_mortal #only necessary for scope_out

Person.greek_and_mortal { find(:all) } vs. Person.greeks.mortal.find(:all)

This is compelling when you have lots of combinations of scopes.

scope_out doesn&#039;t work like a proxy:

Person.find_greek_and_mortal.each { ... } and Person.calculate_greek_and_mortal(:count, ...) vs.
Person.greeks.mortals.each { ... }, Person.count Person.greeks.mortals.count

The lambda form in has_finder is very useful:

Person.gendered(:male).with_hair_color(:purple).count

It works for free with has_many. With scope_out, you have to explicitly say:

&lt;code lang=&quot;ruby&quot;&gt;
class Army
  has_many :people, :extend =&gt; Person::AssociationMethods
end
&lt;/code&gt;

With HasFinder that comes for free:

an_army.people.greeks

Finally, it supports proxy extensions a la:

&lt;code lang=&quot;ruby&quot;&gt;
has_many :foo do
  def my_custom_method
  end
end

has_finder :mortals, :conditions =&gt; ... do
  def my_custom_method
  end
end
&lt;/code&gt;

hence: Person.mortals.my_custom_method

Anyway, hope you enjoy. I believe HasFinder is to be renamed NamedScope; it will be included in Rails 2.1</description>
		<content:encoded><![CDATA[<p>Hi, I&#8217;m the author of has_finder. It&#8217;s much better than scope_out. Scope_out has an old-fashioned API, using nested blocks rather than method chaining:</p>
<p>Person.greeks {  find(:all) } vs. Person.greeks.find(:all)</p>
<p>Scope_out doesn&#8217;t allow composition without a sep declaration, vs. w/ HasFinder it comes for free:</p>
<p>combined_scope :greek_and_mortal #only necessary for scope_out</p>
<p>Person.greek_and_mortal { find(:all) } vs. Person.greeks.mortal.find(:all)</p>
<p>This is compelling when you have lots of combinations of scopes.</p>
<p>scope_out doesn&#8217;t work like a proxy:</p>
<p>Person.find_greek_and_mortal.each { &#8230; } and Person.calculate_greek_and_mortal(:count, &#8230;) vs.<br />
Person.greeks.mortals.each { &#8230; }, Person.count Person.greeks.mortals.count</p>
<p>The lambda form in has_finder is very useful:</p>
<p>Person.gendered(:male).with_hair_color(:purple).count</p>
<p>It works for free with has_many. With scope_out, you have to explicitly say:</p>
<p><pre class="ruby"><span class="kw1">class</span> Army
  has_many <span class="re3">:people</span>, <span class="re3">:extend</span> =<span class="sy0">&amp;</span>gt; <span class="re2">Person::AssociationMethods</span>
<span class="kw1">end</span></pre></p>
<p>With HasFinder that comes for free:</p>
<p>an_army.people.greeks</p>
<p>Finally, it supports proxy extensions a la:</p>
<p><pre class="ruby">has_many <span class="re3">:foo</span> <span class="kw1">do</span>
  <span class="kw1">def</span> my_custom_method
  <span class="kw1">end</span>
<span class="kw1">end</span>
&nbsp;
has_finder <span class="re3">:mortals</span>, <span class="re3">:conditions</span> =<span class="sy0">&amp;</span>gt; ... <span class="kw1">do</span>
  <span class="kw1">def</span> my_custom_method
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></p>
<p>hence: Person.mortals.my_custom_method</p>
<p>Anyway, hope you enjoy. I believe HasFinder is to be renamed NamedScope; it will be included in Rails 2.1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rick</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-21</link>
		<dc:creator>rick</dc:creator>
		<pubDate>Mon, 28 Jan 2008 18:55:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-21</guid>
		<description>Just FYI, but has_finder has been approved for inclusion into Rails 2.1.  We all think it&#039;s a fine plugin.</description>
		<content:encoded><![CDATA[<p>Just FYI, but has_finder has been approved for inclusion into Rails 2.1.  We all think it&#8217;s a fine plugin.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kamal Fariz</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-20</link>
		<dc:creator>Kamal Fariz</dc:creator>
		<pubDate>Mon, 28 Jan 2008 17:27:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-20</guid>
		<description>Pat, how does has_finder differ from the scope_out plugin? Superficially, it looks exactly the same.</description>
		<content:encoded><![CDATA[<p>Pat, how does has_finder differ from the scope_out plugin? Superficially, it looks exactly the same.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: duncanbeevers</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-19</link>
		<dc:creator>duncanbeevers</dc:creator>
		<pubDate>Mon, 28 Jan 2008 08:56:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-19</guid>
		<description>Pat, has_finder looks terrific.

I worry chaining finders togethers like that could pretty easily generate queries that don&#039;t make the best use of mysql&#039;s indices, but nothing deliberate use wouldn&#039;t ease.

The ability to generate these kinds of finders with the lambda syntax in has_finder looks really nice as well.  I&#039;ll have to dig through it to see some of the implementation details.  
Thanks for the heads-up!</description>
		<content:encoded><![CDATA[<p>Pat, has_finder looks terrific.</p>
<p>I worry chaining finders togethers like that could pretty easily generate queries that don&#8217;t make the best use of mysql&#8217;s indices, but nothing deliberate use wouldn&#8217;t ease.</p>
<p>The ability to generate these kinds of finders with the lambda syntax in has_finder looks really nice as well.  I&#8217;ll have to dig through it to see some of the implementation details.<br />
Thanks for the heads-up!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pat Maddox</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-18</link>
		<dc:creator>Pat Maddox</dc:creator>
		<pubDate>Mon, 28 Jan 2008 05:13:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-18</guid>
		<description>By far the most elegant solution I&#039;ve seen is &lt;a href=&quot;http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries&quot; rel=&quot;nofollow&quot;&gt;has_finder&lt;/a&gt; (which I hear has also been accepted as a Rails patch, though under a different name).  It generates a has_many with arguments, but it uses an association proxy.  This means that you can use all the methods available on the original class, or even chain finders.  It will make your life better.</description>
		<content:encoded><![CDATA[<p>By far the most elegant solution I&#8217;ve seen is <a href="http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries" rel="nofollow">has_finder</a> (which I hear has also been accepted as a Rails patch, though under a different name).  It generates a has_many with arguments, but it uses an association proxy.  This means that you can use all the methods available on the original class, or even chain finders.  It will make your life better.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Le</title>
		<link>http://www.dweebd.com/ruby/has_many-with-arguments/comment-page-1/#comment-17</link>
		<dc:creator>Alex Le</dc:creator>
		<pubDate>Sun, 27 Jan 2008 17:52:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.dweebd.com/ruby/has_many-with-arguments/#comment-17</guid>
		<description>I think I found the most elegant solution to this issue (e.g. no hacking or writing custom association).  I was hacking the acts_as_taggable_on_steroids plugin to support different categories for tags and taggings and I bumped right into this issue.

Now that I have a tag with many taggings and they all have to belongs to the same category as the current tag.  How do we do this thru the has_many ... :conditions?

I took the hint from this post to use single quote on the :conditions clause to delay the evaluation of the condition till executing time.


This is the original declaration for the Tag model

&lt;code lang=&quot;ruby&quot;&gt;
class Tag &lt; ActiveRecord::Base
  has_many :taggings
end
&lt;/code&gt;

The modified version with dynamic condition is

&lt;code lang=&quot;ruby&quot;&gt;
class Tag &lt; ActiveRecord::Base
  has_many :taggings, :conditions =&gt; &#039;#{Tagging.table_name}.category_id = #{self.send(:category_id)}&#039;
end
&lt;/code&gt;

Notice the single quote for the conditions, and I used #{self.send(:category_id)} to tell the Tag instance, at runtime, to dynamically invoke the cateogory_id attribute reader.  A quick glance to check on the tail log for development.log confirmed that the query was indeed evaluated at runtime!

No more hacking just pure coding fun!

BTW:  I&#039;m running Rails 1.2.5 still.</description>
		<content:encoded><![CDATA[<p>I think I found the most elegant solution to this issue (e.g. no hacking or writing custom association).  I was hacking the acts_as_taggable_on_steroids plugin to support different categories for tags and taggings and I bumped right into this issue.</p>
<p>Now that I have a tag with many taggings and they all have to belongs to the same category as the current tag.  How do we do this thru the has_many &#8230; :conditions?</p>
<p>I took the hint from this post to use single quote on the :conditions clause to delay the evaluation of the condition till executing time.</p>
<p>This is the original declaration for the Tag model</p>
<p><pre class="ruby"><span class="kw1">class</span> Tag <span class="sy0">&lt;</span> <span class="re2">ActiveRecord::Base</span>
  has_many <span class="re3">:taggings</span>
<span class="kw1">end</span></pre></p>
<p>The modified version with dynamic condition is</p>
<p><pre class="ruby"><span class="kw1">class</span> Tag <span class="sy0">&lt;</span> <span class="re2">ActiveRecord::Base</span>
  has_many <span class="re3">:taggings</span>, <span class="re3">:conditions</span> <span class="sy0">=&gt;</span> <span class="st0">'#{Tagging.table_name}.category_id = #{self.send(:category_id)}'</span>
<span class="kw1">end</span></pre></p>
<p>Notice the single quote for the conditions, and I used #{self.send(:category_id)} to tell the Tag instance, at runtime, to dynamically invoke the cateogory_id attribute reader.  A quick glance to check on the tail log for development.log confirmed that the query was indeed evaluated at runtime!</p>
<p>No more hacking just pure coding fun!</p>
<p>BTW:  I&#8217;m running Rails 1.2.5 still.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic page generated in 0.353 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-28 11:15:08 -->
