# Undefined method - what am I missing?

**URL:** <https://www.ruby-forum.com/t/undefined-method-what-am-i-missing/146201>\
**Category:** Rails\
**Created:** [August 19, 2008, 1:07am UTC](https://www.ruby-forum.com/t/undefined-method-what-am-i-missing/146201 "2008-08-19T01:07:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 19, 2008, 1:07am UTC](https://www.ruby-forum.com/t/undefined-method-what-am-i-missing/146201/1 "2008-08-19T01:07:24Z")

</div>

This has to be another one of those really easy obvious ones, but I just  
can’t figure it out…

I have a boolean field called “public” to designate whether a record in  
my database should be viewable by the public, or only by people signed  
in.

In my controller, I have:  
------------app/controllers/sources\_controller.rb--------------  
class SourcesController \< ApplicationController

…

def find\_public  
Source.find\_by\_live(true)  
end

## def find\_private Source.find\_by\_live(false) end end

## Then, in my view, I have: -------------app/views/users/show.html.erb--------------------- \<% @user.sources.public do |c| %\>
-   
\<%= c.editor %\>,  
_\<%= link\_to c.title, :controller =\> “sources”, :action =\>  
“show\_cat”, :id =\> c.id %\>_  
(\<%= c.publication %\>, \<%= c.pub\_date %\>).  

\<%= link\_to “Edit”, {:controller =\> “sources”, :action =\>  
‘edit’, :id =\> c.id} %\> |  
\<%= link\_to “Delete”, {:controller =\> “sources”, :action =\>  
‘delete’, :id =\> c.id},  
:confirm =\> “Are you sure you want to delete this record  
permanently?” %\>

  
  
\<% end %\>

When I run this, I get a no method error: “undefined method  
`find\_public’”

It doesn’t work in the console either:

> > Source.find\_private  
> > NoMethodError: undefined method `find_private' for #<Class:0x2f36a18> from /usr/pkg/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/base.rb:1667:in`method\_missing’  
> > from (irb):1

I know this is something simple, but I can’t figure it out.

Thanks so much for your help!  
Morgan.

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 19, 2008, 1:15am UTC](https://www.ruby-forum.com/t/undefined-method-what-am-i-missing/146201/2 "2008-08-19T01:15:40Z")

</div>

On Aug 19, 12:07 am, Morgan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> When I run this, I get a no method error: “undefined method  
> `find\_public’”
> 
> It doesn’t work in the console either:\>\> Source.find\_private
> 
> NoMethodError: undefined method `find\_private’ for #Class:0x2f36a18  
> from

Well you’ve defined an instance method on SourcesController, but  
you’re trying to call a class method on Source instead. Not sure how  
you were expecting that to work. public is a magic word in ruby but  
you might be able to get away with it here.

Fred

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 19, 2008, 1:22am UTC](https://www.ruby-forum.com/t/undefined-method-what-am-i-missing/146201/3 "2008-08-19T01:22:57Z")

</div>

> Well you’ve defined an instance method on SourcesController, but  
> you’re trying to call a class method on Source instead. Not sure how  
> you were expecting that to work. public is a magic word in ruby but  
> you might be able to get away with it here.

Yes, I’m new to all of this, and I knew I was doing it wrong… But can  
you help me figure out how to do it right?

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 19, 2008, 1:28am UTC](https://www.ruby-forum.com/t/undefined-method-what-am-i-missing/146201/4 "2008-08-19T01:28:21Z")

</div>

Frederick C. wrote:

> public is a magic word in ruby but  
> you might be able to get away with it here.
> 
> Fred

Right, the boolean field is called “live”, not “public”… sorry for  
that confusion…

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 19, 2008, 1:34am UTC](https://www.ruby-forum.com/t/undefined-method-what-am-i-missing/146201/5 "2008-08-19T01:34:22Z")

</div>

On Aug 19, 12:22 am, Morgan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Well you’ve defined an instance method on SourcesController, but  
> > you’re trying to call a class method on Source instead. Not sure how  
> > you were expecting that to work. public is a magic word in ruby but  
> > you might be able to get away with it here.
> 
> Yes, I’m new to all of this, and I knew I was doing it wrong… But can  
> you help me figure out how to do it right?

Well if you want to be able to say Source.find\_private then you need

class Source \< ActiveRecord::Base  
def self.find\_private  
…  
end  
end

or (equivalently)

class Source \< ActiveRecord::Base  
class \<\< self  
def find\_private  
…  
end  
end  
end

There’s a slightly magic incantation needed to make a class method,  
but if it’s not obvious why defining find\_private on SourcesController  
is completely nonsensical then I suggest you read up on you object  
oriented programming (something like the pickaxe will introduce you to  
that and ruby at the same time)

Fred
