# Agile Rails book: depot problem 2

**URL:** <https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241>\
**Category:** Rails\
**Created:** [February 5, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241 "2006-02-05T12:01:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 12:01pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/1 "2006-02-05T12:01:02Z")

</div>

My second problem is:

Listing users, and deleting them doesn’t work.

If I try to list user, i just get this list:

User Delete  
User Delete  
User Delete  
User Delete

I’m using this code in the login\_controller:

def list\_users  
@all\_users = User.find(:all)  
end

def delete\_user  
@id = params[:id]  
if id && user = User.find(id)  
begin  
User.destroy  
flash[:notice] = “User #{user.name} deleted”  
rescue  
flash[:notice] = “Can’t delete that user”  
end  
end  
redirect\_to\_index(:action =\> :list\_users)  
end

and the list\_users view looks like this:

\<% for users in @all\_users -%\>  
  
\<%= User.name %\>  
\<%= link\_to ‘Delete’, { :action =\> ‘delete\_user’, :id =\> id },  
:confirm =\> ‘Are you sure?’ %\>  
  
\<% end -%\>

trying to delete a user gives:

ActiveRecord::RecordNotFound in Login#delete\_user  
Couldn’t find User with ID=30034968

By the way, I got this errormessage by clicking:

[Delete](https://www.ruby-forum.com/login/delete_user/29113692)

The user ID’s seem to be messed up, and I don’t see what I did wrong…

Johan.

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 2:13pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/2 "2006-02-05T14:13:26Z")

</div>

Hi,

try

\<% for user in @all\_users -%\>

  
\<%= user.name %\>  
\<%= link\_to ‘Delete’, { :action =\> ‘delete\_user’,  
:id =\> user },  
:confirm =\> ‘Are you sure?’ %\>  
  
\<% end -%\>

HTH, Beate

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 2:37pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/3 "2006-02-05T14:37:13Z")

</div>

Thank you, listing users is working now.  
But I still get an error message if I try to delete one:

→ [http://localhost:3001/login/delete\_user/1](http://localhost:3001/login/delete_user/1)

> ActiveRecord::RecordNotFound in Login#delete\_user  
> Couldn’t find User with ID=30085920

Apparantly, I should have used user.name instead of User.name…  
Could you explain me why I shouldn’t use a capital U in User.name?

Thank you,

Johan.

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/4 "2006-02-05T15:39:52Z")

</div>

Your suggested code didn’t work.  
I don’t get an error message anymore,  
but I get a flash notice: actionlist\_users.  
And the user doesn’t get deleted.

However, I managed to fix my problem:

def delete\_user  
id = params[:id]  
if id && user = User.find(id)  
begin  
user.destroy  
flash[:notice] = “User #{user.name} deleted”  
rescue  
flash[:notice] = “Can’t delete that user”  
end  
end  
redirect\_to(:action =\> :list\_users)  
end

Again, I typed User.destroy instead of user.destroy.  
But you can use User.find instead of user.find.  
I still don’t really get it: when do you have to use capital characters  
/ when not?

Beate, thank you for helping me with my 2 problems!

Johan

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/5 "2006-02-05T16:32:15Z")

</div>

2006/2/5, Johan Van M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Again, I typed User.destroy instead of user.destroy.  
> But you can use User.find instead of user.find.  
> I still don’t really get it: when do you have to use capital characters  
> / when not?

User.find(…) means the model, i.e. find all or the first (depending  
on your find) User from the table. user.destroy means the user set in  
“user = User.find(id)”, a special user.

But i can understand you, this was a little bit confusing to me in the  
beginning, too.

Beate

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/6 "2006-02-05T15:10:46Z")

</div>

2006/2/5, Johan Van M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thank you, listing users is working now.  
> But I still get an error message if I try to delete one:
> 
> → [http://localhost:3001/login/delete\_user/1](http://localhost:3001/login/delete_user/1)
> 
> > ActiveRecord::RecordNotFound in Login#delete\_user  
> > Couldn’t find User with ID=30085920

there is a typo in your controller. This does not work:  
@id = params[:id]  
if id && user = User.find(id)

Try this:  
@id = params[:id]  
if @id && user = User.find(@id)

> Apparantly, I should have used user.name instead of User.name…  
> Could you explain me why I shouldn’t use a capital U in User.name?

You can’t use “User”, as this is not definded in the view:  
\<% for user in @all\_users -%\>  
I myself visualize this like “For every single user in @users”, so  
“user” is my variable.

Beate

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/7 "2006-02-05T19:00:12Z")

</div>

It helps a lot!  
Everything is clear now.

So this code:

id = params[:id]  
if id && user = User.find(id)  
begin  
user.destroy  
flash[:notice] = “User #{user.name} deleted”

actually does this:

It creates an instance variable id, based on the specified parameters.  
If this id was created, a user instance variable is created, based on a  
User in the User class, with the specified id.  
Then this user instance variable (thus the User with the id you  
specified) is destroyed, and a flash message appears.

And by typing in User.destroy - if this method existed - ,you would do  
something like deleting all users?

Thank you all for your help! I love the RoR community 🙂

Johan.

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/8 "2006-02-05T17:14:41Z")

</div>

Lowercase “user” is an instance variable… you can use it if you’ve  
created or retrieved  
a User into it (though you could also retrieve any other object into it  
or you could call  
the variable “rumplestiltskin”… but that would be more confusing). The  
uppercase “User”  
is the actual class.

So, when you do User.destroy you are calling a “class method”, meaning  
the method is not  
tied to an instance (a “static” method in Java). This shouldn’t work  
because “destroy” is  
not a class method.

When you do user.destroy you are calling an “instance method”, meaning  
the method is tied  
to a specific instance of User… and it does the work declared in that  
method _to_ the  
instance you call it on. This should delete the row in the db that  
corresponds to the User  
object held in variable “user”.

The destroy method makes sense as an instance method, because you’re  
destroying a specific  
instance. The find method, on the other hand, makes sense as a class  
method because you  
don’t have an instance to call find on until you call find!

Hope that helps.

b

---

<div class="post-metadata">

**Author:** ![johan-vm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@johan-vm](https://www.ruby-forum.com/u/johan-vm)\
**Post date:** [February 5, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/agile-rails-book-depot-problem-2/101241/9 "2006-02-05T20:58:19Z")

</div>

Johan Van M. wrote:

> actually does this:
> 
> It creates an instance variable id, based on the specified parameters.  
> If this id was created, a user instance variable is created, based on a  
> User in the User class, with the specified id.  
> Then this user instance variable (thus the User with the id you  
> specified) is destroyed, and a flash message appears.
> 
> And by typing in User.destroy - if this method existed - ,you would do  
> something like deleting all users?

Well, if User.destroy existed, it might do something like that… but it  
doesn’t. You’ve  
got it though… use class methods (defined by doing “def  
self.methodname…”) to do stuff  
that doesn’t make sense to have attached to a single instance and use  
instance methods to  
do stuff that does.

b
