<?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>Transcending Frontiers &#187; Named Scopes with Joins and Default Block Arguments</title>
	<atom:link href="http://blog.thefrontiergroup.com.au/tag/named-scopes/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thefrontiergroup.com.au</link>
	<description>Your peek inside the collective mind of The Frontier Group</description>
	<lastBuildDate>Mon, 02 Apr 2012 04:32:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Named Scopes with Joins and Default Block Arguments</title>
		<link>http://blog.thefrontiergroup.com.au/2010/05/named-scopes-with-joins-and-default-block-arguments/</link>
		<comments>http://blog.thefrontiergroup.com.au/2010/05/named-scopes-with-joins-and-default-block-arguments/#comments</comments>
		<pubDate>Tue, 04 May 2010 06:19:56 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Named Scopes]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://thefrontiergroup.com.au/blog/?p=723</guid>
		<description><![CDATA[Today I was fiddling with some code to get particular types of payments that are due on particular days and I ran across a couple of things I don&#8217;t want to solve again. Firstly, the problem of being able to have default arguments to a block in ruby. It&#8217;s solved nicely in Ruby 1.9 but [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was fiddling with some code to get particular types of payments that are due on particular days and I ran across a couple of things I don&#8217;t want to solve again.</p>
<p>Firstly, the problem of being able to have default arguments to a block in ruby. It&#8217;s solved nicely in Ruby 1.9 but we&#8217;re using 1.8.x on our boxes at the moment. The work around is incredibly simple though and goes something like this :</p>
<p><code class="ruby"><br />
lambda { |*args|<br />
date = (args[0] || Date.today)<br />
.. remaining code ..<br />
}<br />
</code></p>
<p>That&#8217;s all there is to it really. You could go the whole hog with hashed attributes and so on but I think it starts to get a bit smelly if your anonymous functions are taking more than one argument.</p>
<p>The other issue I had was whether a named scope can include a join, and it can.</p>
<p><code class="ruby"><br />
named_scope :credit_card, { :joins =&gt; :subscription, :conditions =&gt; "subscriptions.method = 'credit_card'" }<br />
</code></p>
<p>You can hash it all out if you want to, though if it&#8217;s all about readability I find the above to be more suitable. However this will also work :</p>
<p><code class="ruby"><br />
... :conditions { :payment_plans =&gt; { :payment_method =&gt; "credit_card" } }<br />
</code></p>
<p>The coolest thing about all of this though (and I feel I&#8217;m very late to the party here) is that I now get to do things like :</p>
<p><code class="ruby"><br />
Payment.credit_card.due_on(Date.today)<br />
# or<br />
Payment.credit_card.due_on<br />
# or<br />
Payment.due_on(Date.today + 1.month)<br />
</code></p>
<p>In the above I had just used the default argument to the due_on named scope to be today&#8217;s date.</p>
<p>I mean, I&#8217;d seen a bunch of tutorials on named scopes and how they worked but hadn&#8217;t found a use case in my work. I think it&#8217;s finally twigged for me though and will be making use of them a bit more in the future. </p>
<script src="http://feeds.feedburner.com/~s/TranscendingFrontiers?i=http://blog.thefrontiergroup.com.au/2010/05/named-scopes-with-joins-and-default-block-arguments/" type="text/javascript" charset="utf-8"></script>]]></content:encoded>
			<wfw:commentRss>http://blog.thefrontiergroup.com.au/2010/05/named-scopes-with-joins-and-default-block-arguments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

