# Populate table from another table

**URL:** <https://www.ruby-forum.com/t/populate-table-from-another-table/77853>\
**Category:** Rails\
**Created:** [December 1, 2006, 12:59am UTC](https://www.ruby-forum.com/t/populate-table-from-another-table/77853 "2006-12-01T00:59:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 1, 2006, 12:59am UTC](https://www.ruby-forum.com/t/populate-table-from-another-table/77853/1 "2006-12-01T00:59:58Z")

</div>

I want to populate one table in a DB from another table so that the  
user does not need to enter the same information twice.

I have a registration table that is populated when a user creates an  
account. Later I have a form that uses a different table, but contains  
most of the same info as the registration table. I would like that  
table to be prepopulated by the registration table before the user gets  
to the form so that they can just update/change that information  
instead of having to enter it all over again.

What is the best way to do this?

Code and pointers to tutorials is always helpful.  
Thanks in advance – K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 1, 2006, 2:32am UTC](https://www.ruby-forum.com/t/populate-table-from-another-table/77853/2 "2006-12-01T02:32:19Z")

</div>

If the two tables are actually storing the same data, I would say you  
need  
to consider a different database layout. But, I believe you are saying  
that  
you want to provide default values for the form, drawn from the users  
table. If that is the case, I you pre-populate an in-memory form model  
with  
info from the users table, and send that to the form view. This will  
then  
have that default data in the web form when it is viewed by the user.  
When  
the form is submitted, you treat it as you would any other form. In  
this  
method, you simply provide default values in the form fields, not in the  
database. Your controller might look something like this:

# @viewer is the user that is logged in and viewing the page, perhaps

set in  
a before\_filter  
def invitation  
@invitation = Invitation.new  
@invitation.address = @viewer.address  
end

Hope that helps.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 1, 2006, 11:35am UTC](https://www.ruby-forum.com/t/populate-table-from-another-table/77853/3 "2006-12-01T11:35:22Z")

</div>

On 30 Nov 2006, at 23:58, Kim wrote:

> What is the best way to do this?
> 
> Code and pointers to tutorials is always helpful.  
> Thanks in advance – K

Have a look at this post by Jamis B.:

[http://weblog.jamisbuck.org/2006/11/7/don-t-be-afraid-of-harnessing-sql](http://weblog.jamisbuck.org/2006/11/7/don-t-be-afraid-of-harnessing-sql)

Regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 1, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/populate-table-from-another-table/77853/4 "2006-12-01T19:11:09Z")

</div>

Let me explain a little more.

I have a user table that has\_many pages which has\_many page\_modules.  
There are different types of page\_modules - an instructor\_module , a  
librarian \_module, etc…

The user registers as either an instructor or a librarian - hence why I  
have to store duplicate information in the user and the instructor/  
librarian table.

Take this example: A librarian creates an account and fills out  
information about themselves which is stored in the user table. They  
create a page and then start  
creating the page\_modules. They come to the librarian \_module. I want  
the system to recognize that they are a librarian (per the role field  
in the user table) and populate the librarian \_module form from the  
user table which will then be saved to the librarian \_module.

If I was to use foreign keys linking the user table to the librarian  
\_module table instead of duplicating the data in two tables - how will  
an instructor create the librarian \_module or vice versa. Do you see my  
point?

So again how can I preload data from one table to another table before  
the forms view is displayed?

I would also accept help on my database scheme if I am way off base in  
my thinking.

Thanks – K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 1, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/populate-table-from-another-table/77853/5 "2006-12-01T23:25:57Z")

</div>

I’m not quite sure I understand the data duplication problem in your  
schema. But I think the solution you are looking for might be an  
after\_create hook in the User model. If you define a method in your  
User  
model named after\_create, it will be run after the model is ‘created’,  
in  
other words, after it has been inserted into the database for the first  
time. In this method, you might want to do something like:

class User \< ActiveRecord::Base  
…  
def after\_create  
case self.role  
when “librarian”  
LibrarianModule.new do |mod|  
# populate the mod object and save it  
mod.user = self  
…  
mod.save!  
end  
when “instructor”  
… # something similar  
end  
end  
…  
end

In this solution, creating a use automatically results in the creation  
of a  
corresponding LibrarianModule when the User model is first created.  
Depending on you project, it might make more sense to use an  
after\_create  
method in the LibrarianModule. Hopefully this helps.

jkit

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [December 2, 2006, 12:42am UTC](https://www.ruby-forum.com/t/populate-table-from-another-table/77853/6 "2006-12-02T00:42:09Z")

</div>

I think the after\_create method will work. Thanks.
