# Simple attribute copy

**URL:** <https://www.ruby-forum.com/t/simple-attribute-copy/92939>\
**Category:** Rails\
**Created:** [April 25, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/simple-attribute-copy/92939 "2007-04-25T21:43:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah\_Jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sarah\_Jackson](https://www.ruby-forum.com/u/Sarah_Jackson)\
**Post date:** [April 25, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/simple-attribute-copy/92939/1 "2007-04-25T21:43:04Z")

</div>

Lol don’t ask why but I would like to use to code I found on the forum  
to copy two values from table A to table B. It looks a bit roundabout  
but it doesn’t matter to me.

Okay so I know that this part goes in the controller class(i dont think  
I have to modify it at all?):

def copy\_attributes(dest, src, \*attrs)  
for attr in attrs  
dest.send(attr.to\_s + ‘=’,src.send(attr))  
end  
end

Then where does this line of code go/how do I execute the copy? Would I  
include it in def new, or between the “ends” in the above code, or wrap  
it in \<=% %\> tags in the view.

copy\_attributes(bidule, biniou, :foo, :bar, :baz)

---

<div class="post-metadata">

**Author:** ![Sarah\_Jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sarah\_Jackson](https://www.ruby-forum.com/u/Sarah_Jackson)\
**Post date:** [May 4, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/simple-attribute-copy/92939/2 "2007-05-04T18:57:03Z")

</div>

Hey I just decided to start tackling this one again. Can anybody  
point me in the right direction?

Sarah Jackson wrote:

> Lol don’t ask why but I would like to use to code I found on the forum  
> to copy two values from table A to table B. It looks a bit roundabout  
> but it doesn’t matter to me.
> 
> Okay so I know that this part goes in the controller class(i dont think  
> I have to modify it at all?):
> 
> def copy\_attributes(dest, src, \*attrs)  
> for attr in attrs  
> dest.send(attr.to\_s + ‘=’,src.send(attr))  
> end  
> end
> 
> Then where does this line of code go/how do I execute the copy? Would I  
> include it in def new, or between the “ends” in the above code, or wrap  
> it in \<=% %\> tags in the view.
> 
> copy\_attributes(bidule, biniou, :foo, :bar, :baz)

---

<div class="post-metadata">

**Author:** ![Sarah\_Jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@Sarah\_Jackson](https://www.ruby-forum.com/u/Sarah_Jackson)\
**Post date:** [May 4, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/simple-attribute-copy/92939/3 "2007-05-04T19:14:03Z")

</div>

On May 4, 2007, at 12:57 PM, Sarah Jackson wrote:

> > Then where does this line of code go/how do I execute the copy?  
> > Would I  
> > include it in def new, or between the “ends” in the above code, or  
> > wrap  
> > it in \<=% %\> tags in the view.
> > 
> > copy\_attributes(bidule, biniou, :foo, :bar, :baz)

You definitely shouldn’t be changing models in the view.

Depending on what you’re trying to do, you might be better off  
putting your attribute copying code in your model, along the lines of:

class ModelA \< ActiveRecord::Base  
end

class ModelB \< ActiveRecord::Base

def self.create\_from\_a(model\_a)  
# Set this model’s attributes from other model’s attributes  
save  
end  
end

Then in your controller you can call:

class MyController \< Application  
def my\_action  
@my\_a = ModelA.find(params[:id])  
@my\_b = ModelB.create\_from\_a(@my\_a)  
end  
end

James.

–  
James S.  
[http://jystewart](http://jystewart),net/process/
