# Simple thing about an undefined method throwing me

**URL:** <https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215>\
**Category:** Rails\
**Created:** [November 16, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215 "2006-11-16T19:55:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 16, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215/1 "2006-11-16T19:55:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 16, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215/2 "2006-11-16T20:07:12Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 16, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215/3 "2006-11-16T20:21:15Z")

</div>

cool, thanks very much  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 16, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215/4 "2006-11-16T22:35:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 16, 2006, 10:45pm UTC](https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215/5 "2006-11-16T22:45:58Z")

</div>

yep, that worked.  
thanks so much !

sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 16, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/simple-thing-about-an-undefined-method-throwing-me/76215/6 "2006-11-16T20:39:18Z")

</div>

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
