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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11/18/06, nephish <nephish-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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?''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 Jones mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org President/Technical Director, Acme Art Company (acmeartco.org) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
this is great, i will try it out. thanks sk On 11/18/06, Matt Jones <mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On 11/18/06, nephish <nephish-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > 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? > > > > ''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 Jones > mdj.acme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > President/Technical Director, Acme Art Company ( acmeartco.org) > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 18, 12:54 pm, "shawn bright" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> this is great, i will try it out. > thanks > sk > > On 11/18/06, Matt Jones <mdj.a...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > On 11/18/06, nephish <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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? > > > ''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 Jones > > mdj.a...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 18, 1:02 pm, "nephish" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 18, 12:54 pm, "shawn bright" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > this is great, i will try it out. > > thanks > > sk > > > On 11/18/06, Matt Jones <mdj.a...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > On 11/18/06, nephish <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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? > > > > ''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 Jones > > > mdj.a...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > > > 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 ? > skoh wait, it is working, thanks Matt, for your help on this. sk --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---