<?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: Wrapping Conditional Access with with_scope</title>
	<atom:link href="http://codecolossus.com/2008/02/02/wrapping-conditional-access-with-with_scope/feed/" rel="self" type="application/rss+xml" />
	<link>http://codecolossus.com/2008/02/02/wrapping-conditional-access-with-with_scope/</link>
	<description>Software Craftsmanship with Ruby</description>
	<lastBuildDate>Wed, 28 Dec 2011 17:54:32 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: benhughes</title>
		<link>http://codecolossus.com/2008/02/02/wrapping-conditional-access-with-with_scope/comment-page-1/#comment-10</link>
		<dc:creator>benhughes</dc:creator>
		<pubDate>Wed, 27 Feb 2008 14:07:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.railsgarden.com/2008/02/02/wrapping-conditional-access-with-with_scope/#comment-10</guid>
		<description>has_many :conditions is not a solution to this problem and I&#039;m not sure you fully understood the problem my post is trying to solve.  In my original code, forcing the requirement of active = 1 on ALL queries involving Entry within the controller is completely handled by the around filter and with_scope in a most AOP manner.  I&#039;m not looking for a elegant way of getting active only entries as such a solution would require explicitly saying &quot;something.entries.active&quot;.  The above is completely transparent to the rest of the controller except of course if you are manually invoking find_by_conditions.

But you raise a point about with_scope being protected.  Just as easily I could have defined a method in entry that handled this:

&lt;pre name=&quot;code&quot; class=&quot;ruby&quot;&gt;
def self.scope_to_active
  self.send(:with_scope, :find =&gt; {:conditions =&gt; &#039;active = 1&#039;}) do  
    yield  
  end  
end
&lt;/pre&gt;

.. and then called it from the controller.  This is probably a better solution. If you want to use this with multiple models you could even create a module and mix it in since the above code is generic so long as the model has an &quot;active&quot; attribute.  You can then nest-scope multiple of these.</description>
		<content:encoded><![CDATA[<p>has_many :conditions is not a solution to this problem and I&#8217;m not sure you fully understood the problem my post is trying to solve.  In my original code, forcing the requirement of active = 1 on ALL queries involving Entry within the controller is completely handled by the around filter and with_scope in a most AOP manner.  I&#8217;m not looking for a elegant way of getting active only entries as such a solution would require explicitly saying &#8220;something.entries.active&#8221;.  The above is completely transparent to the rest of the controller except of course if you are manually invoking find_by_conditions.</p>
<p>But you raise a point about with_scope being protected.  Just as easily I could have defined a method in entry that handled this:</p>
<pre name="code" class="ruby">
def self.scope_to_active
  self.send(:with_scope, :find => {:conditions => 'active = 1'}) do
    yield
  end
end
</pre>
<p>.. and then called it from the controller.  This is probably a better solution. If you want to use this with multiple models you could even create a module and mix it in since the above code is generic so long as the model has an &#8220;active&#8221; attribute.  You can then nest-scope multiple of these.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Neil Wilson</title>
		<link>http://codecolossus.com/2008/02/02/wrapping-conditional-access-with-with_scope/comment-page-1/#comment-9</link>
		<dc:creator>Neil Wilson</dc:creator>
		<pubDate>Wed, 27 Feb 2008 11:43:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.railsgarden.com/2008/02/02/wrapping-conditional-access-with-with_scope/#comment-9</guid>
		<description>with_scope is protected for a reason - people abuse it instead of creating associations properly using the modelling tools. It is a &#039;code smell&#039; that suggests you are using a Rails anti-patterns.

&quot;has_many :conditions&quot; is your friend, or perhaps something like this:

http://weblog.jamisbuck.org/2007/1/9/extending-activerecord-associations

Try it between the User and Entry models and see what you come up with.</description>
		<content:encoded><![CDATA[<p>with_scope is protected for a reason &#8211; people abuse it instead of creating associations properly using the modelling tools. It is a &#8216;code smell&#8217; that suggests you are using a Rails anti-patterns.</p>
<p>&#8220;has_many :conditions&#8221; is your friend, or perhaps something like this:</p>
<p><a href="http://weblog.jamisbuck.org/2007/1/9/extending-activerecord-associations" rel="nofollow">http://weblog.jamisbuck.org/2007/1/9/extending-activerecord-associations</a></p>
<p>Try it between the User and Entry models and see what you come up with.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

