# Why instance variable in destroy?

**URL:** <https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687>\
**Category:** Rails\
**Created:** [May 15, 2008, 10:34am UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687 "2008-05-15T10:34:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 10:34am UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/1 "2008-05-15T10:34:42Z")

</div>

Ãn Rails 2.0 the scaffold generator (for e.g. a model called “Context”)  
creates the destroy method like this:

def destroy  
@context = Context.find(params[:id])  
@context.destroy

```
respond_to do |format|
  format.html { redirect_to(contexts_url) }
  format.xml { head :ok }
end

```

end

My question is: Is there any special reason why @context is defined as  
an Instance variable and not just a local variable? It is not used in  
any views afterwards - the method always redirects to another action. So  
would this be equally valid:

def destroy  
context = Context.find(params[:id])  
context.destroy  
…

- Carsten

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/2 "2008-05-15T17:51:18Z")

</div>

hmm… good question  
I am going to guess that a local (procedure level) variable would not  
have access to class instance methods that are needed.

Someone please correct me if that is wrong.

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 5:56pm UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/3 "2008-05-15T17:56:21Z")

</div>

i don’t think it’s necessary  
maybe it’s just convenient for ppl who want to give some last message  
like  
“the record with the id #{@context.id} was destroyed”

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 5:58pm UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/4 "2008-05-15T17:58:25Z")

</div>

On 15 May 2008, at 16:50, Ruby F. wrote:

> hmm… good question  
> I am going to guess that a local (procedure level) variable would not  
> have access to class instance methods that are needed.
> 
> Someone please correct me if that is wrong.

Nope. The only reason why you’d use an instance variable would be if  
the view was going to do something with it, or perhaps if you were  
going to call some other instance method of your controller that  
wanted access.

My guess is that it’s just for consistency with show/edit/update  
etc… (which do need it for the view)

Fred

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/5 "2008-05-15T20:16:06Z")

</div>

Yes, you could use a local variable. However, how would you write a  
test to  
ensure that local variable is being populated with the correct type of  
data? It’s much easier to write a test for an instance variable.

James

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/6 "2008-05-15T20:31:01Z")

</div>

James H. wrote:

> Yes, you could use a local variable. However, how would you write a  
> test to  
> ensure that local variable is being populated with the correct type of  
> data? It’s much easier to write a test for an instance variable.

Ahh James… Now that was a clever answer 🙂

Maybe I just shouldn’t think so much over this - I know it is  
nitpicking, typically me to wonder about such things.

- Carsten

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/7 "2008-05-15T20:36:44Z")

</div>

It’s a very good question though. It shows you’re actually thinking  
about  
what you’re doing. If you’re just a RoR-robot you’re not going to get  
anywhere.

Keep asking questions 🙂

James

---

<div class="post-metadata">

**Author:** ![tuktuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/77aa72/32.png) [@tuktuk](https://www.ruby-forum.com/u/tuktuk)\
**Post date:** [May 15, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/why-instance-variable-in-destroy/137687/8 "2008-05-15T20:38:29Z")

</div>

On May 15, 7:15 pm, “James H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes, you could use a local variable. However, how would you write a test to  
> ensure that local variable is being populated with the correct type of  
> data? It’s much easier to write a test for an instance variable.

Although given that the action is destroy, the more thorough test  
would be to assert the specified record is no longer in the database,  
and for that the instance variable is irrelevant.

Fred
