<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Wrapping Conditional Access with with_scope</title>
	<link>http://www.railsgarden.com/2008/02/02/wrapping-conditional-access-with-with_scope/</link>
	<description>A delicious blend of Ruby and Rails...</description>
	<pubDate>Fri, 21 Nov 2008 03:23:21 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.2</generator>
		<item>
		<title>By: benhughes</title>
		<link>http://www.railsgarden.com/2008/02/02/wrapping-conditional-access-with-with_scope/#comment-10</link>
		<dc:creator>benhughes</dc:creator>
		<pubDate>Wed, 27 Feb 2008 14:07:14 +0000</pubDate>
		<guid>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'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'm not looking for a elegant way of getting active only entries as such a solution would require explicitly saying "something.entries.active".  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="code" class="ruby"&gt;
def self.scope_to_active
  self.send(:with_scope, :find =&gt; {:conditions =&gt; 'active = 1'}) 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 "active" 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://www.railsgarden.com/2008/02/02/wrapping-conditional-access-with-with_scope/#comment-9</link>
		<dc:creator>Neil Wilson</dc:creator>
		<pubDate>Wed, 27 Feb 2008 11:43:15 +0000</pubDate>
		<guid>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 'code smell' that suggests you are using a Rails anti-patterns.

"has_many :conditions" 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 - 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>
