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
end

end

and in my view:

<% @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)%>" %>


<% 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

site current status
<%= site.has_pressure_sensor %>

def has_pressure_sensor
y_n = ‘n’
self.sites.each do |site|
if site.option = ‘pressure’
y_n = ‘y’
end
end
end

Hrm… 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

cool, thanks very much
sk

Hey,

comments below:

On 16-Nov-06, at 11:37 AM, nephish wrote:

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

yep, that worked.
thanks so much !

sk

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