Model not found?

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

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

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs