# Need one step deeper into a search and verify

**URL:** <https://www.ruby-forum.com/t/need-one-step-deeper-into-a-search-and-verify/76410>\
**Category:** Rails\
**Created:** [November 18, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/need-one-step-deeper-into-a-search-and-verify/76410 "2006-11-18T16:46:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 18, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/need-one-step-deeper-into-a-search-and-verify/76410/1 "2006-11-18T16:46:50Z")

</div>

hello all.

i have a model with a model called site.rb  
i have a method in it that will check to see if any of the options for  
this site has a pressure sensor.

def has\_pressure\_sensor?  
self.site\_options.any? {|option| option.option == ‘pressure’}  
end

now, site belongs\_to :group.  
i keep screwing up when i try to write a function that checks to see  
if any of the sites that belong to a group has the sensor.

i tried like this  
@group.sites.each do |site|  
site.has\_pressure\_sensor?

i was thinking that if any of the sites returned true, that i would  
have the true/false there. But it isn’t working right.

any suggestions?

thanks  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 18, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/need-one-step-deeper-into-a-search-and-verify/76410/2 "2006-11-18T19:57:16Z")

</div>

this is great, i will try it out.  
thanks  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 18, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/need-one-step-deeper-into-a-search-and-verify/76410/3 "2006-11-18T19:53:14Z")

</div>

On 11/18/06, nephish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> have the true/false there. But it isn’t working right.
> 
> any suggestions?

‘inject’ will do what you want here - your code would be:

@group.sites.inject(false) { | result, site | result ||  
site.has\_pressure\_sensor? }

Note that this is somewhat optimized - once one site has returned true,  
has\_pressure\_sensor?  
will not be evaluated for the remaining sites.

Hope this helps,

–  
Matt J.  
removed\_email\_address@domain.invalid  
President/Technical Director, Acme Art Company ([acmeartco.org](http://acmeartco.org))

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 18, 2006, 8:55pm UTC](https://www.ruby-forum.com/t/need-one-step-deeper-into-a-search-and-verify/76410/4 "2006-11-18T20:55:21Z")

</div>

On Nov 18, 1:02 pm, “nephish” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > On 11/18/06, nephish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > Hope this helps,
> 
> > > –  
> > > Matt J.  
> > > removed\_email\_address@domain.invalid  
> > > President/Technical Director, Acme Art Company ( [acmeartco.org](http://acmeartco.org))ok, i put it in and it didn’t fail out, but it is evaluating true every  
> > > time. Even if i select a site that does not have one. Is there  
> > > something i am missing ?  
> > > sk

oh wait, it is working, thanks Matt, for your help on this.  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 18, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/need-one-step-deeper-into-a-search-and-verify/76410/5 "2006-11-18T20:05:16Z")

</div>

On Nov 18, 12:54 pm, “shawn bright” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > hello all.  
> > > i keep screwing up when i try to write a function that checks to see
> 
> > –  
> > Matt J.  
> > removed\_email\_address@domain.invalid  
> > President/Technical Director, Acme Art Company ( [acmeartco.org](http://acmeartco.org))

ok, i put it in and it didn’t fail out, but it is evaluating true every  
time. Even if i select a site that does not have one. Is there  
something i am missing ?  
sk
