# Variable/reference question

**URL:** <https://www.ruby-forum.com/t/variable-reference-question/108267>\
**Category:** Rails\
**Created:** [July 1, 2007, 11:56am UTC](https://www.ruby-forum.com/t/variable-reference-question/108267 "2007-07-01T11:56:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [July 1, 2007, 11:56am UTC](https://www.ruby-forum.com/t/variable-reference-question/108267/1 "2007-07-01T11:56:22Z")

</div>

Hi!

I feel stupid for having to ask this, but I have never run into this  
before…

I have this code that should send an email when someone upgrades their  
account. I am using this code:

new\_plan = Plan.find(params[:customer][:plan\_id])  
previous\_plan = @customer.plan  
@customer.change\_plan(new\_plan)  
@customer.save!  
ElectronicMailer.deliver\_notify\_upgrade(@customer.name,  
@customer.url, @customer.plan.name, previous\_plan.name,  
@customer.billing\_id, @customer.start\_date) # if new\_plan.price \>  
previous\_plan.price

The problem is an email never gets send, because new\_plan.price is  
always equal to previous\_plan.price, so it apparently previous\_plan  
holds a reference to the customer’s plan and when I change that,  
previous\_plan changes too.

When I change: previous\_plan = @customer.plan  
to: previous\_plan = Plan.find(@customer.plan\_id)

it does work, but it feels like I don’t need the extra find…

How would you guys do this?

Thanks,  
Mischa.

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [July 1, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/variable-reference-question/108267/2 "2007-07-01T13:14:12Z")

</div>

Yes, previous\_plan is a reference to @customer\_plan and so changes  
with it. Instead of

> previous\_plan = @customer.plan

This should work with something like

previous\_plan = @customer.plan.dup

Which makes previous\_plan a copy rather than a reference.

Though not a huge deal, this to me is one of the more annoying and  
confusing bits of Ruby. It uses references in many places where I’d  
think it would just copy values. And the difference between dup and  
clone is poorly documented or mysterious or both.
