# Refactoring question

**URL:** <https://www.ruby-forum.com/t/refactoring-question/187327>\
**Category:** Rails\
**Created:** [April 21, 2010, 7:32pm UTC](https://www.ruby-forum.com/t/refactoring-question/187327 "2010-04-21T19:32:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 21, 2010, 7:32pm UTC](https://www.ruby-forum.com/t/refactoring-question/187327/1 "2010-04-21T19:32:06Z")

</div>

Hi, guys. I changed some columns types to decimal in database, but the  
code is not DIY. Any thoughts on refactoring the following code?

change\_table :accounts do |t|  
t.change :check\_amount, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
t.change :cash\_amount, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
t.change :change\_amount, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
.  
.  
.  
.  
t.change :total, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
end

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 21, 2010, 7:59pm UTC](https://www.ruby-forum.com/t/refactoring-question/187327/2 "2010-04-21T19:59:32Z")

</div>

On Apr 21, 2010, at 10:32 AM, Ichiro S. wrote:

> :change\_amount, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
> .  
> .  
> .  
> .  
> t.change :total, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
> end

change\_table :accounts do |t|  
[:check\_amount, :cache\_amount, :change\_amount, …, :total].each do  
|c|  
t.change :c, :decimal, :precision =\> 6, :scale =\> 2, :default =\> 0  
end  
end

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 21, 2010, 8:10pm UTC](https://www.ruby-forum.com/t/refactoring-question/187327/3 "2010-04-21T20:10:04Z")

</div>

On Apr 21, 2010, at 10:56 AM, Philip H. wrote:

> > :check\_amount, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
> > end
> 
> change\_table :accounts do |t|  
> [:check\_amount, :cache\_amount, :change\_amount, …, :total].each do  
> |c|  
> t.change :c, :decimal, :precision =\> 6, :scale =\> 2, :default =\> 0

Oops… that should be:

t.change c, …

(no colon before the c)

---

<div class="post-metadata">

**Author:** ![bluegreen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8491ac/32.png) [@bluegreen](https://www.ruby-forum.com/u/bluegreen)\
**Post date:** [April 21, 2010, 9:03pm UTC](https://www.ruby-forum.com/t/refactoring-question/187327/4 "2010-04-21T21:03:39Z")

</div>

Philip H. wrote:

> On Apr 21, 2010, at 10:56 AM, Philip H. wrote:
> 
> > > :check\_amount, :decimal, :precision=\>6, :scale=\>2, :default=\>0  
> > > end
> > 
> > change\_table :accounts do |t|  
> > [:check\_amount, :cache\_amount, :change\_amount, …, :total].each do  
> > |c|  
> > t.change :c, :decimal, :precision =\> 6, :scale =\> 2, :default =\> 0
> 
> Oops… that should be:
> 
> t.change c, …
> 
> (no colon before the c)

Thank you for your help. It’s much cleaner now.
