need one step deeper into a search and verify

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

‘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,

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

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