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

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

On 11/18/06, nephish [email protected] 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.
[email protected]
President/Technical Director, Acme Art Company (acmeartco.org)

On Nov 18, 1:02 pm, “nephish” [email protected] wrote:

On 11/18/06, nephish [email protected] wrote:

Hope this helps,


Matt J.
[email protected]
President/Technical Director, Acme Art Company ( 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

On Nov 18, 12:54 pm, “shawn bright” [email protected] wrote:

hello all.
i keep screwing up when i try to write a function that checks to see


Matt J.
[email protected]
President/Technical Director, Acme Art Company ( 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs