# DRY a named scope

**URL:** <https://www.ruby-forum.com/t/dry-a-named-scope/190680>\
**Category:** Rails\
**Created:** [June 25, 2010, 12:35am UTC](https://www.ruby-forum.com/t/dry-a-named-scope/190680 "2010-06-25T00:35:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 25, 2010, 12:35am UTC](https://www.ruby-forum.com/t/dry-a-named-scope/190680/1 "2010-06-25T00:35:20Z")

</div>

Is there a way to DRY this up a bit?

named\_scope :has\_valid\_sysoid, lambda{|sysoid|  
(sysoid.nil?) ? {:conditions =\> [“nodesysoid IS NOT NULL AND  
nodelabel LIKE ‘%-to-%’”], :include =\> [:ipinterface, :alarm]} :  
{:conditions =\> ["nodesysoid IS NOT NULL AND nodelabel LIKE ‘%-to-%’  
AND nodesysoid = ? ", sysoid], :include =\> [:ipinterface, :alarm]}  
}

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 25, 2010, 1:09am UTC](https://www.ruby-forum.com/t/dry-a-named-scope/190680/2 "2010-06-25T01:09:54Z")

</div>

Thanks.

Trying to keep the controller simple:  
Takes a value or not.  
One thing I could do was put he include inside the method call but  
where does the param go?

Node.has\_valid\_sysoid(…)

The scope is working, just trying cut out the duplication.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 25, 2010, 12:58am UTC](https://www.ruby-forum.com/t/dry-a-named-scope/190680/3 "2010-06-25T00:58:46Z")

</div>

On Jun 24, 2010, at 3:34 PM, Me wrote:

> Is there a way to DRY this up a bit?
> 
> named\_scope :has\_valid\_sysoid, lambda{|sysoid|  
> (sysoid.nil?) ? {:conditions =\> [“nodesysoid IS NOT NULL AND  
> nodelabel LIKE ‘%-to-%’”], :include =\> [:ipinterface, :alarm]} :  
> {:conditions =\> ["nodesysoid IS NOT NULL AND nodelabel LIKE ‘%-to-%’  
> AND nodesysoid = ? ", sysoid], :include =\> [:ipinterface, :alarm]}  
> }

You could move the condition inside the {}'s so it only affects the  
value of the :conditions key.

But… I would be more included to separate the scopes into  
:has\_valid\_sysoid and :has\_specific\_sysoid.

Model.has\_valid\_sysoid(‘foo’) doesn’t really seem to be doing what the  
scope suggests it’s doing… to me anyway… I see a difference b/n  
“valid” and “a specific”.

I don’t know if that makes sense for your application or not…

-philip

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [July 22, 2010, 3:52pm UTC](https://www.ruby-forum.com/t/dry-a-named-scope/190680/4 "2010-07-22T15:52:33Z")

</div>

named\_scope :has\_valid\_sysoid, lambda{|sysoid|  
conditions = [“nodesysoid IS NOT NULL AND nodelabel LIKE ‘%-to-%’]  
unless sysoid.nil?  
conditions[0]\<\< " AND nodesysoid = ?”  
conditions \<\< sysoid  
end  
{:conditions =\> conditions, :include =\> [:ipinterface, :alarm]}  
}

\> \>\> \> } \>\> \> For more options, visit this group at \> http://groups.google.com/group/rubyonrails-talk?hl=en. \> \>

–  
Herr Bente P.,  
Weidenallee 37  
20357, Hamburg

Tel.: 0152-22361035
