# Controller method problem

**URL:** <https://www.ruby-forum.com/t/controller-method-problem/102283>\
**Category:** Rails\
**Created:** [August 14, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/controller-method-problem/102283 "2006-08-14T17:52:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 14, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/controller-method-problem/102283/1 "2006-08-14T17:52:10Z")

</div>

Hi,

I have a def in my controller see below where i want to find the page  
object based on a parameter value and then use the page\_id to then find  
all the contents that belong to that page in the same method.

Controller  
def aboutus  
@page = Page.find\_by\_name(params[‘About Us’])  
@contents = Content.find(:all,  
:conditions =\> “page\_id = #{@page.id}”)  
end

Page Model  
def self.find\_by\_name(pagename)  
find(:first,  
:conditions =\> “name = ‘#{pagename}’”)  
end

I keep getting an error message “Called id for nil”. Can anyone let me  
know what i’m doing wrong here.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/controller-method-problem/102283/2 "2006-08-15T14:20:16Z")

</div>

On 8/14/06, Jeff C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> returns all matches.  
> For all the gory details…

[http://blog.hasmanythrough.com/articles/2006/08/13/how-dynamic-finders-work](http://blog.hasmanythrough.com/articles/2006/08/13/how-dynamic-finders-work)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 14, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/controller-method-problem/102283/3 "2006-08-14T20:23:59Z")

</div>

John B. wrote:

> Hi,
> 
> I have a def in my controller see below where i want to find the page  
> object based on a parameter value and then use the page\_id to then find  
> all the contents that belong to that page in the same method.
> 
> Controller  
> def aboutus  
> @page = Page.find\_by\_name(params[‘About Us’])  
> @contents = Content.find(:all,  
> :conditions =\> “page\_id = #{@page.id}”)  
> end

> I keep getting an error message “Called id for nil”. Can anyone let me  
> know what i’m doing wrong here.

Sounds like @page is nil. Are you sure you params[‘About Us’] contains  
a valid page name?

> Page Model  
> def self.find\_by\_name(pagename)  
> find(:first,  
> :conditions =\> “name = ‘#{pagename}’”)  
> end

Nice, but Rails already did this for you 🙂 ActiveRecord objects will  
create dynamic finders on the fly for you if you follow a certain  
convention:

model.find\_by\_column(‘value\_to\_search\_for’)

returns the first match, and

model.find\_all\_by\_column(‘value\_to\_search\_for’)

returns all matches.

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 15, 2006, 2:20pm UTC](https://www.ruby-forum.com/t/controller-method-problem/102283/4 "2006-08-15T14:20:48Z")

</div>

On 8/14/06, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a def in my controller see below where i want to find the page  
> object based on a parameter value and then use the page\_id to then find  
> all the contents that belong to that page in the same method.
> 
> Controller  
> def aboutus  
> @page = Page.find\_by\_name(params[‘About Us’])

Do you really have a param named “About Us”? Are you sure you aren’t  
trying to pass “About Us” into that method?

– James

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 15, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/controller-method-problem/102283/5 "2006-08-15T16:34:53Z")

</div>

James L. wrote:

> On 8/14/06, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have a def in my controller see below where i want to find the page  
> > object based on a parameter value and then use the page\_id to then find  
> > all the contents that belong to that page in the same method.
> > 
> > Controller  
> > def aboutus  
> > @page = Page.find\_by\_name(params[‘About Us’])
> 
> Do you really have a param named “About Us”? Are you sure you aren’t  
> trying to pass “About Us” into that method?
> 
> – James

Thanks guys.

Yes your right, the param is not called About Us i was just hard coding  
that value just to make sure the value i wanted was being passed to the  
model. There was nothing coming from the model thats why i was getting  
the nil error message.

@page = Page.find(:first,:conditions =\> “name = ‘About Us’”)  
@contents = Content.find(:all,  
:conditions =\> “page\_id = #{@page.id}”,  
:order =\> ‘pos, position\_id’)
