# Find works in view but not controller

**URL:** <https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182>\
**Category:** Rails\
**Created:** [February 28, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182 "2007-02-28T18:21:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [February 28, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/1 "2007-02-28T18:21:05Z")

</div>

Part of my application should show a list of all users on the system who  
agreed to have their details revealed. So, I have an action like this:

def view\_users  
@users = User.find(:all,:conditions=\>“reveal = 1”,:order=\>“login  
DESC”)  
end

In the associated view, @users is an object of nilclass and the app.  
breaks as it tries to iterate over the @users array. However, if I  
include this at the top of view\_users.rhtml

\<% @users = User.find(:all,:conditions=\>“reveal = 1”,:order=\>“login  
DESC”) %\>

…then everything works as expected.Why should this be?  
Thanks.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 2, 2007, 1:13am UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/2 "2007-03-02T01:13:13Z")

</div>

> include this at the top of view\_users.rhtml
> 
> \<% @users = User.find(:all,:conditions=\>“reveal = 1”,:order=\>“login  
> DESC”) %\>
> 
> …then everything works as expected.Why should this be?

My guess is that somewhere (perhaps an after\_filter or a partial) you’re  
setting @users to nil.

What happens if in your controller right after that find() call you do:

render :text =\> @users.inspect, :layout =\> false  
return

Do you get all your users then?

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 2, 2007, 10:24am UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/3 "2007-03-02T10:24:37Z")

</div>

Philip H. wrote:

> What happens if in your controller right after that find() call you do:
> 
> render :text =\> @users.inspect, :layout =\> false  
> return
> 
> Do you get all your users then?

That works - many thanks.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 2, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/4 "2007-03-02T12:31:12Z")

</div>

Hello there,

I ran into a problem with my shiny new RESTful controller.  
One of it’s partial templates is using a custom member action’s  
url helper created by map.resources, e.g. “something\_model\_url(id)”.  
This works perfectly when I try it in my webbrowser, but my functional  
test fails when it gets to “display” the mentioned partial, telling  
me:  
Exception: undefined method “something\_model\_url” for #\<#\<Class…\>  
On line #22 of (…)

Obviously the helper does not get defined. I already know that the  
routing is not being processed in functional tests due to performance  
reasons, but there must be a way to still access my partials?! So  
I guess I am searching for the magic “bring me some url-helpers” trigger  
in my functional tests. Any suggestions will be greatly appreciated!

Greetings,  
Christoph

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 2, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/5 "2007-03-02T17:10:46Z")

</div>

> Philip H. wrote:
> 
> > What happens if in your controller right after that find() call you do:
> > 
> > render :text =\> @users.inspect, :layout =\> false  
> > return
> > 
> > Do you get all your users then?
> 
> That works - many thanks.

Then somewhere after your find() call, but before your view you are  
trashing @users…

😕

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 2, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/6 "2007-03-02T18:21:55Z")

</div>

Just to add a quick note about a ‘gotcha’ that bit me in the early  
days…

Philip H. wrote:

> Then somewhere after your find() call, but before  
> your view you are trashing @users…

If you’re doing a redirect\_to, that’ll trash your instance variables.

Best regarsd,  
Bill

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 2, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/7 "2007-03-02T18:25:49Z")

</div>

Bill W. wrote:

> If you’re doing a redirect\_to, that’ll trash your instance variables.

All fixed now, thanks.  
This has been useful at improving my understanding of how rails works.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 6, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/find-works-in-view-but-not-controller/87182/8 "2007-03-06T16:15:24Z")

</div>

Hi Folks,

I still can’t figure out what the problem could be. It seems it’s  
only the singular\_url helpers that are missing, the plural\_url helpers  
do not lead to test failure. Did really no one else run into something  
like this?

Greetings,  
Christoph

Christoph O. schrieb:
