# Model not found?

**URL:** <https://www.ruby-forum.com/t/model-not-found/71365>\
**Category:** Rails\
**Created:** [October 7, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/model-not-found/71365 "2006-10-07T19:18:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/model-not-found/71365/1 "2006-10-07T19:18:14Z")

</div>

I have a view template “list\_users.rhtml”. On one line, I have

\<% @users = User.find(:all) %\>

in my models directory I have a file user.rb which starts with the line

class User \< ActiveRecord::Base

Problem: When I invoke list\_user.rhtml, I’m getting the error  
“Uninitialized constant User” which points at the User.find line listed  
above. It’s my understanding that find is built into the ActiveRecord  
class, so I think this is implying that it doesn’t see my class. Why?  
What else might I need to do to make sure my model file gets loaded?

thanks in advance  
—Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/model-not-found/71365/2 "2006-10-07T20:36:33Z")

</div>

Hi Michael,

Michael S. wrote:

> I have a view template “list\_users.rhtml”. On one line,  
> I have  
> \<% @users = User.find(:all) %\>

You should be doing this in your controller, not your view.

> Problem: When I invoke list\_user.rhtml,

What to you mean “invoke”?

Bill

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/model-not-found/71365/3 "2006-10-07T20:48:43Z")

</div>

Bill W. wrote:

> Hi Michael,  
> Problem: When I invoke list\_user.rhtml,
> 
> What to you mean “invoke”?

Bad choice of words. Actually rails invokes it when the list\_users  
action in the controller is called.

> Bill

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/model-not-found/71365/4 "2006-10-07T21:56:41Z")

</div>

Michael S.

> Bill W. wrote:
> 
> > Hi Michael,  
> > Problem: When I invoke list\_user.rhtml,
> > 
> > What to you mean “invoke”?
> 
> Bad choice of words. Actually rails invokes it when the list\_users  
> action in the controller is called.

Have you tried moving the find into the controller? If so and you’re  
still  
having problems, you’ll need to supply more info.

Sorry I can’t be more help yet.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [October 7, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/model-not-found/71365/5 "2006-10-07T22:18:43Z")

</div>

On Sat, 2006-10-07 at 19:18 +0200, Michael S. wrote:

> above. It’s my understanding that find is built into the ActiveRecord  
> class, so I think this is implying that it doesn’t see my class. Why?  
> What else might I need to do to make sure my model file gets loaded?

you model class is fine the way it is.

in app/controllers/UserController.rb you should have a method that has  
the same name as the template you want rails to automagically call. This  
method needs to pass the template a class variable (preceded by a ‘@’)  
the template can have access to:

def list\_users # same name as template  
@all\_users = User.find(:all) # note @ before var name  
end # no need to invoke anything

then in app/views/users/list\_users.rhtml you can access the collection  
of users via the class variable @all\_users

example:

 \<% foreach user in @all\_users %\> - \<%= user.name 
 \<% end %\>
