# Named\_scope multiple conditions

**URL:** <https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696>\
**Category:** Rails\
**Created:** [July 16, 2010, 12:46pm UTC](https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696 "2010-07-16T12:46:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [July 16, 2010, 12:46pm UTC](https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696/1 "2010-07-16T12:46:59Z")

</div>

Hi everyone,

I have a working named\_scope here, however it’s too cluttered.  
Anyone got a better (more efficient) and concise alternative?

Thanks.

============================================  
named\_scope :filter, lambda { |\*args|  
if  
args.first && args.second == nil  
{} # return all records

```
       if args.first.blank?
           { :conditions => ["status = ?", args.second] }

       if
         args.second.blank?
           { :conditions => ["invoice_number= ?", args.first] }

     else
       { :conditions => ["nvoice_number= ? and status = ?",

```

args.first, args.second] }  
end  
end  
end  
}

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [July 16, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696/2 "2010-07-16T13:10:59Z")

</div>

On Jul 16, 11:42 am, Jermaine [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everyone,
> 
> I have a working named\_scope here, however it’s too cluttered.  
> Anyone got a better (more efficient) and concise alternative?
> 
> Thanks.

well named\_scope :filter, lambda {|\*args|  
{:conditions =\> {:status =\> args.first, :invoice\_number =\>  
args.second}.reject{|k,v| v.blank?}}  
}

is more compact and i think does the same thing as your code. With  
anything like this any time you save when building up the conditions  
will be dwarfed by the time it takes to run the actual query

Fred

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [July 16, 2010, 2:14pm UTC](https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696/3 "2010-07-16T14:14:50Z")

</div>

Hi Fred,

Thanks for the suggestion. However what did you mean specifically with  
your last comment?

> > With anything like this any time you save when building up the conditions  
> > will be dwarfed by the time it takes to run the actual query

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [July 16, 2010, 2:34pm UTC](https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696/4 "2010-07-16T14:34:10Z")

</div>

aaah I-C.  
Okay thanks for clearing that up! great stuff

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [July 16, 2010, 2:57pm UTC](https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696/5 "2010-07-16T14:57:00Z")

</div>

You might want to split those two into named\_scopes of their own  
though - for more modularity and possible scope chaining opportunities  
elsewhere in the code.  
Handling whether one of those arguments, which I presume are user  
entered, is blank can be handled in the controller.

---

<div class="post-metadata">

**Author:** ![Jermaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Jermaine](https://www.ruby-forum.com/u/Jermaine)\
**Post date:** [July 16, 2010, 2:17pm UTC](https://www.ruby-forum.com/t/named-scope-multiple-conditions/191696/6 "2010-07-16T14:17:46Z")

</div>

> Thanks for the suggestion. However what did you mean specifically with  
> your last comment?
> 
> > > With anything like this any time you save when building up the  
> > > conditions
> > 
> > > will be dwarfed by the time it takes to run the actual query

Putting words in Frederick’s mouth, but it simply means that it’s  
ridiculously quick to build up the conditions object in Ruby compared to  
actually executing the SQL on the database (and communicating the SQL  
and  
response over the socket).

Cheers,

Andy
