# Copying Records

**URL:** <https://www.ruby-forum.com/t/copying-records/98198>\
**Category:** Rails\
**Created:** [July 13, 2006, 12:56am UTC](https://www.ruby-forum.com/t/copying-records/98198 "2006-07-13T00:56:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [July 13, 2006, 12:56am UTC](https://www.ruby-forum.com/t/copying-records/98198/1 "2006-07-13T00:56:41Z")

</div>

This might seem like a silly quesiton but it’s been baffling me all day!

I want to copy all records matching certain criteria from table A to  
table B.

I know how to do his with SQL but not with RoR.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [July 13, 2006, 1:19am UTC](https://www.ruby-forum.com/t/copying-records/98198/2 "2006-07-13T01:19:55Z")

</div>

Well, you could still do it with SQL:

ActiveRecord::Base.connection.execute( )

(may want to check for actual method names and the like, buts its  
available)

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [July 13, 2006, 1:13am UTC](https://www.ruby-forum.com/t/copying-records/98198/3 "2006-07-13T01:13:46Z")

</div>

Hi John,

John H. wrote:

> I want to copy all records matching certain criteria  
> from table A to table B.
> 
> I know how to do his with SQL but not with RoR.

I’m assuming you _want_ to do this with RoR 😉

If that’s the case, the simplest way is to do a .find(:all) and then  
save  
each record in the returned array. Base case looks something like…

records = A\_singular\_version\_of\_table\_name.find(:all)  
records.each |record|  
#assign fields in A to fields in B  
B\_singular\_version\_of\_table\_name.field1 = record.field1  
#etc.  
B.save  
end

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [July 13, 2006, 2:10am UTC](https://www.ruby-forum.com/t/copying-records/98198/4 "2006-07-13T02:10:14Z")

</div>

Cheers Bill,

That worked a treat once I figured out to put in a ‘do’ before the  
|record|

Many thanks

John

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [July 13, 2006, 2:18am UTC](https://www.ruby-forum.com/t/copying-records/98198/5 "2006-07-13T02:18:46Z")

</div>

Oops. Sorry about that 😉

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [January 1, 2007, 3:37pm UTC](https://www.ruby-forum.com/t/copying-records/98198/6 "2007-01-01T15:37:21Z")

</div>

If you have two different tables than you would have two different  
classes (models) to work with. Let say that you have two classes  
OldThing and NewThing. You want to copy all of the records from the  
old\_things table to the new\_things table. You would do it like this:

old\_things = OldThing.find(:all, :conditions =\> my\_sql\_where\_clause)  
old\_things.each do |old\_thing|  
new\_thing.create(old\_thing.attributes)  
end

This would go through each of the records in the old\_things table and  
add them to the new\_things table assuming the field names matched up.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [January 1, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/copying-records/98198/7 "2007-01-01T14:33:06Z")

</div>

Bill W. wrote:

> Hi John,
> 
> John H. wrote:
> 
> > I want to copy all records matching certain criteria  
> > from table A to table B.
> > 
> > I know how to do his with SQL but not with RoR.
> 
> I’m assuming you _want_ to do this with RoR 😉
> 
> If that’s the case, the simplest way is to do a .find(:all) and then  
> save  
> each record in the returned array. Base case looks something like…
> 
> records = A\_singular\_version\_of\_table\_name.find(:all)  
> records.each |record|  
> #assign fields in A to fields in B  
> B\_singular\_version\_of\_table\_name.field1 = record.field1  
> #etc.  
> B.save  
> end
> 
> hth,  
> Bill

Can someone give a working example of this code, i dont really  
understand it. i just want to copy data from selected fields from table  
A to table B just like in the code.
