Hey there, i am having some trouble with a method in a model ( i am new to rails and ruby ) comming from python so anyway, here is what i have in my Site.rb model: class Site < ActiveRecord::Base belongs_to :group belongs_to :status_sensor has_many :site_options def has_pressure_sensor y_n = ''n'' self.sites.each do |site| if site.option = ''pressure'' y_n = ''y'' end end end end and in my view: <table> <th>site</th> <th>current status</th> <% @group.sites.each do |site| %> <tr> <td><%= site.site_name %></td> <td> <%= site.status_sensor.page_value %> <img src="<%fetch_circle_for(site.status_sensor.page_value)%>" %> </td> <td> <%= site.has_pressure_sensor %> </td> </tr> <% end %> this is what i get: undefined method `has_pressure_sensor'' for #<Site:0xb750e6ac> i just want it to print out a y or n for yes or no if you have read this far, thank you for your time. shawn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2006-Nov-16 19:12 UTC
Re: simple thing about an undefined method throwing me
> Hey there, > i am having some trouble with a method in a model ( i am new to rails > and ruby ) comming from python > > so anyway, here is what i have in my Site.rb model: > > class Site < ActiveRecord::Base > belongs_to :group > belongs_to :status_sensor > has_many :site_options > > > def has_pressure_sensor > y_n = ''n'' > self.sites.each do |site| > if site.option = ''pressure'' > y_n = ''y'' > end > end > endHrm... I don''t know the rest of your models, but guessing I think your method should look more like this if only because your calling ''self.sites'' which according to your model above a site doesn''t have sites... def has_pressure_sensor? self.site_options.each do |option| return true if option == ''pressure'' end false end That will return true if one of the site_options for that site equals ''pressure'' otherwise false. The ''?'' in the name is a ruby convetion when you''re querying truth/false-ness. Then in your view you could do: <%= site.has_pressure_sensor? ? ''yes'' : ''no'' %> As for why it''s not being seen, perhaps you are running in production mode? Or for some other reason need to restart your rails app. -philip> > end > > and in my view: > <table> > <th>site</th> > <th>current status</th> > > <% @group.sites.each do |site| %> > <tr> > <td><%= site.site_name %></td> > <td> > <%= site.status_sensor.page_value %> > <img src="<%> fetch_circle_for(site.status_sensor.page_value)%>" %> > </td> > <td> > <%= site.has_pressure_sensor %> > </td> > </tr> > > <% end %> > > this is what i get: > > undefined method `has_pressure_sensor'' for #<Site:0xb750e6ac> > > i just want it to print out a y or n for yes or no > > if you have read this far, thank you for your time. > shawn > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
cool, thanks very much 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 -~----------~----~----~----~------~----~------~--~---
woops, it isn''t working right. it is printing no on all , even the ones that should print yes. in my SiteOption model i have belongs_to :site and in Sites.rb model i have has_many :site_options i have checked the options for the ones i am listing and two of the 6 have the option of pressure in the site_options table. anything else i may have left out ? 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 -~----------~----~----~----~------~----~------~--~---
Trevor Squires
2006-Nov-16 20:48 UTC
Re: simple thing about an undefined method throwing me
Hey, comments below: On 16-Nov-06, at 11:37 AM, nephish wrote:> > woops, it isn''t working right. > > it is printing no on all , even the ones that should print yes. > > in my SiteOption model i have belongs_to :site > and in Sites.rb model i have > has_many :site_options > > i have checked the options for the ones i am listing and two of the 6 > have the option of pressure in the site_options table. > > anything else i may have left out ? >I assume that you''re using the code for has_pressure_sensor that Phil Hallstrom put in his email - right? def has_pressure_sensor? self.site_options.each do |option| return true if option == ''pressure'' end false end Looking at that (and your has_many :site_options) then of *course* it always returns false. return true if option == ''pressure'' Except the option isn''t a String, it''s a SiteOption so the comparison is always false. You haven''t said what the members of site_option are but I''ll take a guess and say you want something more like this: def has_pressure_sensor? self.site_options.any? {|option| option.name == ''pressure''} end HTH, Trevor> 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 -~----------~----~----~----~------~----~------~--~---
yep, that worked. thanks so much ! 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 -~----------~----~----~----~------~----~------~--~---