# Copying variables between rows

**URL:** <https://www.ruby-forum.com/t/copying-variables-between-rows/142022>\
**Category:** Rails\
**Created:** [July 2, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/copying-variables-between-rows/142022 "2008-07-02T18:03:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MalHayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MalHayn](https://www.ruby-forum.com/u/MalHayn)\
**Post date:** [July 2, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/copying-variables-between-rows/142022/1 "2008-07-02T18:03:35Z")

</div>

Is there an elegant way of copying some variables from one row to  
another row?

Here is the current code:

color = Color.find(id, :select =\> “a, b, c, d, e, f”)  
copy = Color.find(copy, :select =\> “c, d, e”)

color.c = copy.c  
color.d = copy.d  
color.e = copy.e

color.update

---

<div class="post-metadata">

**Author:** ![MalHayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MalHayn](https://www.ruby-forum.com/u/MalHayn)\
**Post date:** [July 2, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/copying-variables-between-rows/142022/2 "2008-07-02T18:20:00Z")

</div>

On 2 Jul 2008, at 16:10, MalHayn wrote:

> color.d = copy.d  
> color.e = copy.e

don’t know if it’s a good idea but  
color.attributes = copy.attributes  
would probably do the trick

Fred

---

<div class="post-metadata">

**Author:** ![MalHayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MalHayn](https://www.ruby-forum.com/u/MalHayn)\
**Post date:** [July 2, 2008, 6:55pm UTC](https://www.ruby-forum.com/t/copying-variables-between-rows/142022/3 "2008-07-02T18:55:45Z")

</div>

On Jul 2, 2008, at 12:19 PM, Frederick C. wrote:

> > color.d = copy.d  
> > color.e = copy.e
> 
> don’t know if it’s a good idea but  
> color.attributes = copy.attributes  
> would probably do the trick
> 
> Fred

You probably need to be aware that using :select causes the Color  
objects to be marked readonly and cannot be saved. Add  
a :readonly=\>false if you want to save the Color object again.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![MalHayn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@MalHayn](https://www.ruby-forum.com/u/MalHayn)\
**Post date:** [July 6, 2008, 9:22am UTC](https://www.ruby-forum.com/t/copying-variables-between-rows/142022/4 "2008-07-06T09:22:31Z")

</div>

Thanks Fred,

color.attributes = copy.attributes did the trick.

Your help has saved hundreds of lines of code.

Previously I tried .clone, .dub, .reject, .to\_hsh, .hash, and more.  
I knew there was a very simple answer.

Thanks Rob,

That is a good point.  
In the working code “select” is not used on color, only copy.

Mark
